Android 从sd卡读取pdf文件
我想读取存储在sd卡中的pdf文件,我尝试使用此代码段Android 从sd卡读取pdf文件,android,Android,我想读取存储在sd卡中的pdf文件,我尝试使用此代码段 File file = new File(Environment.getExternalStorageDirectory() + "/vvveksperten" + "/ypc.pdf"); PackageManager packageManager = getPackageManager(); Intent testIntent = new Intent(Intent.ACTION_VIE
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 market下载pdf阅读器,但请确保您的设备已预安装了android market应用程序。所以我想在android设备上试试,而不是在emulator上试试。与iOS设备不同,许多(或者可能大多数)android设备没有默认的PDF查看器。这就是为什么你会得到例外。没有向正确的IntentFilter注册任何意图
另请参见另一个SO问题:
解决方案很简单:安装一个类似Google Play的PDF查看器。手机上是否安装了任何第三方api来读取PDF????
File file = new File(“/sdcard/read.pdf”);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),”application/pdf”);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);