I intend opening a PDF file with the default application of my device. I have a problem giving address from the assets folder.
My giving address code is as follows:
File pdfFile = new File("file:///android_assets/test.pdf");
Uri path = Uri.fromFile(pdfFile);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setDataAndType(path, "application/pdf");
startActivity(intent);
This code gives the error This path is not valid when Adobe Reader opens. What's the correct address?
It shows the error because only your application have the permission to access that folder. You can think to copy the file in a different directory (e.g. some place in the sdcard) and provide acrobat reader with the uri to the copied file,
Related
when i m run it i m getting this error like "File or folder not found" and " file dosnt exit or can't be opened "..
This is the code sir..
i want the how to view pdf files from assets or in raw folder sir.. its very imp for my project try to slove my problem sir.. and tell me clearly i m new to android sir.. can any one send me code cloerly how to do it..
enter code here
Uri path = Uri.parse("android.resource:///com.example.pratice/raw/em");
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 Viewer", Toast.LENGTH_SHORT).show();
}
Few, if any, Android PDF viewer apps will honor the android.resource scheme, and there is no Uri scheme for assets.
If you want to serve up a PDF file, copy it from assets or a raw resource into internal storage, then use FileProvider to make it available to other apps. Here is a sample application that does this.
I need to open a PDF. I know the procedure, this is my code:
string file_path = _path + url.Substring (5);
if (System.IO.File.Exists(file_path)) {
Android.Net.Uri pdfFile = Android.Net.Uri.FromFile (new Java.IO.File (file_path));
Intent pdfIntent = new Intent (Intent.ActionView);
pdfIntent.SetDataAndType (pdfFile, "application/pdf");
pdfIntent.SetFlags (ActivityFlags.NoHistory);
_parent.StartActivity (pdfIntent);
return true;
}
I'm using Xamarin, and the path exists because i check it as you can see.
The app opens Adobe reader, but when it starts an error message shows up saying (File not found). So, my file is in
/data/data/com.myapp/files/.hide/contents/file_test.pdf
Are there some permission to set? I really don't understand why it can't open my file!
The folder /data/data/com.myapp is the private folder of your application. So no other application can access to the content of this folder (in this case Adobe reader).
Instead try to put your pdf into a SDCard folder.
I have created a pdf file in my application and successfully stored that in sdCard, now i want to store the pdf not in my sdcard but in Internal storage as shown here but when i open an intent for pdf readers as:
Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
Log.v("Path",file.getAbsolutePath()); // Path data/data/com.cloudchowk.his.patient/files/LIPID PROFILE1300000403.pdf
i.setDataAndType(uri, "application/pdf");
startActivity(i);
The PDF reader shows an error The document path is not valid.
I have tried this on two devices one was rooted and one was not, but it was not working on any of them. What should i do ? any ideas?
Get the path and create URI
File internalFile = getFileStreamPath("pdf path");
Uri file = Uri.fromFile(internalFile);
Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setDataAndType(file, "application/pdf");
startActivity(i);
To make a file from internal storage available to other apps the cleanest approach is to use a content provider.
Luckily, Android comes with FileProvider – an implementation that might just serve your needs.
You don't need to implement code, you just need to specifiy the provider in your manifest.
I want to open a pdf file saved in application data folder using pdf viewer application. How can i make accessible data folder content to other application. Some code snippet will help me lot.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType( Uri.parse("content://" + pdf_path), "application/pdf" );
startActivity(intent);
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);
You can access pdf file from external memory by this.make sure you are giving correct path of it.
First of all make sure your .pdf file has, MODE_WORLD_READABLE Permission like,
Now to access .pdf file from application data folder means, /data/data/<package_Name>/
Use, getFilesDir() which returns the path of Application data directory,
Then,
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(getFilesDir()+ "pdf_path")),"application/pdf" );
startActivity(intent);
I have a doc file on sdcard.
I want to open it in read-only mode. Also I don't want the document viewer to save the file.
The intent to open the file is as follows:
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File("/sdcard/test.doc");
file.setReadOnly();
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION );
intent.setDataAndType(Uri.fromFile(file), "application/msword");
startActivity(intent);
But when the file opens in quickOffice, the file is still editable.
Please suggest how should I make the file ready-only to any document viewer and how should I prevent file from saving.
It is probably because your app does not have root access. File permissions can be changed by root access only.