Reputation: 1009
I want to read a pdf file stored in my sd card, I tried using this snippet
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);
but it gave me an error.
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 }
Upvotes: 3
Views: 7969
Reputation: 2846
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);
Upvotes: 1
Reputation: 14941
Many (or perhaps most) Android devices don't come with a default PDF Viewer, unlike iOS devices. That is why you are getting the Exception. There's no Intent registered with the right IntentFilter.
Please also see this other SO question: Does android have a built-in PDF viewer?.
The solution is simple: install a PDF Viewer like Adobe Reader from Google Play.
Upvotes: 0
Reputation: 109237
Please check your device is any pdf reader application available, I think isn't any..
Just use this code,
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();
}
}
If any pdf reader application not available then this code is download pdf reader from android market, But be sure your device has pre-installed android-market application. So I think try this on android device, rather then emulator.
Upvotes: 5