I have downloaded a PDF file into my application in the path getFilesDir().getPath()
When I try to open the same file using below code :
String path = getBaseContext().getFilesDir().getPath()+"/myfile.pdf";
File file = new File(path);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
I am getting The target file doesnot exist error from pdf reader.
When I browse my device for myfile.pdf path, It's located in "/sdcard/Android/data/com.***.***/files/data/com.***.***/files/myfile.pdf" .
If I hardcode the above path then also I am getting the same error.
Please let me know how to read file from that path.
use: Environment.getExternalStorageDirectory().getAbsolutePath()
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/myfile.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
Related
I want to open pdf in Android
I am currently using code
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + path.get((int) v.getTag()));
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
mContext.startActivity(intent);
But the file is not opening currently.
Regards
how to get pdf from internal storage in android Using WebView or AnyConcept. i have to get pdf in an activity from internal storage.Can anyone of u guys let me know about this .Thank You.
String filename = null;
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +"/"+ filename);
Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(Uri.fromFile(file),"application/pdf");
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Intent intent = Intent.createChooser(target, "Open File");
startActivity(intent);
and give pdf filename
I know how to share file from SD Card, but this time i have to share file from assets folder.
See below code, which i used to share file from SD
String fileToShare = Environment.getExternalStorageDirectory().getPath() +"/Downloads/" + fileName;
newFile = new File(fileToPrint);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("audio/mp3");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(newFile));
startActivity(intent);
How come every time i try to open a pdf file in my SDCARD using the following code, it doesn't actually open the pdf file itself, but goes into the menu of adobe reader? Is there anything wrong with my code?
Intent intent = new Intent();
File pdfFile = new File("/sdcard/sample.pdf");
Uri path = Uri.fromFile(pdfFile);
intent.setAction(Intent.ACTION_VIEW);
intent.setData(path);
intent.setType("application/pdf");
intent.setPackage("com.adobe.reader");
startActivity(intent);
No, there's nothing wrong. You've set the type to pdf and specified the package as adobe.reader. What that does is fire off an intent to launch the pdf in adobe reader. There's no way to display a pdf directly in your app without using a library (or writing code yourself) to render PDFs and display them.
Note that if you only set the type and not the package, the system will find whatever app is available to display PDFs
EDIT
You can try something like
Intent intent = new Intent(Intent.ACTION_VIEW);
File file = new File( filename );
intent.setDataAndType( Uri.fromFile( file ), "application/pdf" );
startActivity(intent);
or
Intent intent = new Intent();
intent.setPackage("com.adobe.reader");
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
startActivity(intent);
Here I am giving my pdf file name. You give your file name.
private static String FILE = Environment.getExternalStorageDirectory().getPath()+"/TestPDF.pdf";
File file = new File(FILE);
if (file.exists()) {
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
Toast.makeText(this,
"No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}
}
I want to show user my apk file using intents, here is the code:
SDCardRoot = Environment.getExternalStorageDirectory();
File file = new File(SDCardRoot,fileName);
if (file.exists()) {
Uri path = Uri.fromFile(file);
Intent intent = new
Intent(Intent.ACTION_VIEW,path);
startActivity(intent);
but it is giving me ActivityNotFoundException,I want to start the apk file so that user can install it.
Try this?
Uri path = Uri.fromFile(file);
Intent intent = new
Intent(Intent.ACTION_VIEW,path);
intent.setDataAndType(path, "application/vnd.android.package-archive");
startActivity(intent);