gpt4 book ai didi

android - 从sd卡读取pdf文件

转载 作者:太空宇宙 更新时间:2023-11-03 11:38:16 25 4
gpt4 key购买 nike

我想读取存储在我的 sd 卡中的 pdf 文件,我尝试使用这个 fragment

    File file = new File(Environment.getExternalStorageDirectory()
+ "/vvveksperten" + "/ypc.pdf");

PackageManager packageManager = getPackageManager();
Intent testIntent = new Intent(Intent.ACTION_VIEW);
testIntent.setType("application/pdf");
List list = packageManager.queryIntentActivities(testIntent,
PackageManager.MATCH_DEFAULT_ONLY);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/pdf");
startActivity(intent);

但它给了我一个错误。

ERROR/AndroidRuntime(2611): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///mnt/sdcard/vvveksperten/ypc.pdf typ=application/pdf }

最佳答案

请检查您的设备是否有可用的 pdf 阅读器应用程序,我认为没有..

只需使用这段代码,

private void viewPdf(Uri file) {
Intent intent;
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(file, "application/pdf");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
// No application to view, ask to download one
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("No Application Found");
builder.setMessage("Download one from Android Market?");
builder.setPositiveButton("Yes, Please",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent marketIntent = new Intent(Intent.ACTION_VIEW);
marketIntent
.setData(Uri
.parse("market://details?id=com.adobe.reader"));
startActivity(marketIntent);
}
});
builder.setNegativeButton("No, Thanks", null);
builder.create().show();
}
}

如果任何 pdf 阅读器应用程序不可用,则此代码是从 android 市场下载 pdf 阅读器,但请确保您的设备已预安装 android-market 应用程序。所以我想在 android 设备上试试这个,而不是模拟器。

关于android - 从sd卡读取pdf文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9765520/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com