Reputation: 39
Here is the code:
Uri path = Uri.parse("file:///android:asset/Shadi_kiraat.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
I am trying to access pdf file from assets folder but getting error.
E/AndroidRuntime(1325): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///android:asset/Sa.pdf typ=application/pdf flg=0x4000000 }
Upvotes: 0
Views: 1450
Reputation: 607
Try change this "Uri path = Uri.parse("file:///android:asset/Shadi_kiraat.pdf");" to "Uri path = Uri.formFile("file:///android:asset/Shadi_kiraat.pdf");"
Upvotes: 0
Reputation: 59
If you are getting "ActivityNotFoundException", this means your device does not have a pdf viewer installed.
So, in that case you can redirect the user to download a prf viewer.
Here's a sample code:
/**
* Show pdf.
*
* @param context the context
* @param fileName the file name
*/
public static void showPDF(Context context, String fileName) {
try {
Intent intentUrl = new Intent(Intent.ACTION_VIEW);
intentUrl.setDataAndType(getFileUri(context, fileName), "application/pdf");
intentUrl.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intentUrl);
} catch (ActivityNotFoundException e) {
Toast.makeText(context, "No PDF Viewer Installed", Toast.LENGTH_LONG).show();
context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=com.adobe.reader")));
}
}
Also, in my case, I was unable to open a pdf file directly from assets folder. So, I copied the pdf file from the assets folder to the phone SD card before viewing.
Here's the code snippet for coping a file from assets folder to SDCard and return the new file URL.
/**
* Gets the file uri.
*
* @param context the context
* @param fileName the file name
* @return the file uri
*/
private static Uri getFileUri(Context context, String fileName) {
String path = Environment.getExternalStorageDirectory().getPath() + File.separator + "myAppFolder" + File.separator;
File file = new File(path, fileName);
if (!file.exists() || file.length() == 0) {
OutputStream outputStream = null;
InputStream inputStream = null;
try {
File dir = new File(path);
if (!dir.exists()) {
dir.mkdir();
}
new File(path, ".nomedia").createNewFile();
outputStream = new FileOutputStream(file);
inputStream = context.getAssets().open("docs/" + fileName);
byte[] buffer = new byte[1024];
int read;
while ((read = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, read);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
inputStream.close();
inputStream = null;
outputStream.flush();
outputStream.close();
outputStream = null;
} catch (Exception e) {
e.printStackTrace();
}
}
}
return Uri.fromFile(file);
}
Upvotes: 0
Reputation: 356
I think you are trying to access pdf file with other application like adobe reader, if you dont have adobe reader then it gives this kind of error
Install Adobe reader and try again it will work without error
Upvotes: 0
Reputation: 5368
If you get an ActivityNotFoundException
is because there are no application to handle that kind of content. So, in that case, you can Adobe Reader
or any third party pdf viewers to be installed.
Upvotes: 0
Reputation:
This means that in your device PDF supported file not installed. So when you have run your application you are getting such a Exception.
E/AndroidRuntime(1325): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///android:asset/Sa.pdf typ=application/pdf flg=0x4000000 }
So you can use this
Uri path = Uri.parse("file:///android:asset/Shadi_kiraat.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try{
startActivity(intent);
} catch (ActivityNotFoundException ex){
Toast.makeText(this, "No activity found", Toast.LENGTH_LONG).show(); //Display an error message
}
Upvotes: 1