I have to ship pdf files along with my application and display them as list and when clicked each one should open in Adobe reader. I have tried placing the pdf files in /assets folder also in /res/raw folder. But in either case I am encountering ActivityNotFound Exception. The code and the exception message are below.
Placing file in /assets/pdf folder
try {
files = amanager.list("pdf");
//AssetFileDescriptor fd = amanager.openFd(files[0]);
Uri path = Uri.parse("android.resource://com.csg.android.myproject/assets/pdf/csgsample") ;
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setPackage("com.adobe.reader");
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e){
Toast.makeText(FirstTab.this, "NO Viewer", Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Placing file in /res/raw folder
Uri.parse("android.resource://com.csg.android.myproject/raw/csgsample") ;
Exception:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=android.resource://com.csg.android.myproject/assets/pdf/csgsample typ=application/pdf flg=0x4000000 pkg=com.adobe.reader }
Any help would be greatly appreciated.
Thanks in Advance,
Navin
If you get an ActivityNotFoundException is because there are no application to handle that kind of content. So, in that case, I'm sure the the Adobe Reader is installed.
Also, I think you must copy the file to the SDCard for other apps to access it. They cannot access your private data directly for security reasons.
I think assets and raw files are only viewable by your application. 3rd party applications (bundled or not) cannot access these files. You should first put your files in the device's sdcard before opening them.
I have the same problem, but I should note that this is how I open by resource id
Uri.parse("android.resource://com.csg.android.myproject/" + R.raw.csgsample);
Related
I need to open a specific folder in Android 10, so that only the files inside that folder can be seen in my app.
I have tried this:
try {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
File f = new File(getExternalFilesDir(null) + "/MyFolder");
Uri uri = FileProvider.getUriForFile(getApplicationContext(),getApplicationContext().getPackageName()+".provider",f);
Toast.makeText(MainActivity.this,f.getPath(),Toast.LENGTH_LONG).show();
intent.setDataAndType(uri,"text/plain");
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
}
catch (Exception e) {
Log.d("Exception",e.toString());
Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_LONG).show();
}
But, it opens the entire tree.
What I actually want is to open that folder. The files in the folder should be openable by other apps. Any help will be appreciated.
I need to open a specific folder in Android 10, so that only the files inside that folder can be seen in my app
Sorry, but that is not an option.
The closest thing to what you want is EXTRA_INITIAL_URI. However:
It takes a document or tree Uri that you obtained previously from the Storage Access Framework, not a FileProvider Uri; and
It does not prevent users from navigating elsewhere — it just controls the starting point
Hi boys and girls(of course)
I have this method to open a file
public static void openFile(String filePath, Activity activity) {
try {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse("file://"+filePath));
activity.startActivityForResult(intent, 10);
}catch (Exception e){
e.printStackTrace();
Toast.makeText(context, "Error abriendo."+filePath+" Abrir manualmente", Toast.LENGTH_LONG).show();
}
}
but when I sets the file path to a image.jpg saved in my sdcard it's always try to open with PDF-Drive and it's makes an error try open this kind of file.
but when I open the file directly from a filemanager it's opens with a default imageViewer.
What I'm doing wrong?
Regards
You're currently not providing a mime type with the file, so it's likely opening with whatever your system default has been set to. Have you tried to use Intent.setDataAndType(...), and used image/* as the mime type?
I tried to compile in java (NetBeans) a lib.jar but when I imported it to the android project it said that I could not find the Path class and the other that gave me error
I solved by creating my own getMimeType
At https://www.sitepoint.com/web-foundations/mime-types-complete-list/ I found a fairly complete list
I made a
HashMap<String, String> ~ <ext,mime>
And to get the mime to the hash that contains all I ask me of the value of the extension
map.get(".bmp");
Can anyone help me. I want to read pdf files from app/src/main/res/Unit1.pdf.My code is
File file1=new File("app/src/main/res/Unit1.pdf")
Uri path1=Uri.fromFile(file1);
if (file1.exists())
{
Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path1, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e){
Toast.makeText(MainActivity.this,"No Application Availabel To View
PDF",Toast.LENGTH_SHORT).show();
}
}
if section not working....plz help..
You need to write the your PDF file present in your app's res folder to sdcard. So that other apps can read and open it.
As in res folder file is locally available to your app only. and you are Throwing an intent for other apps to open that pdf which is not accessable to them. so remove your if check, write your pfd from res to sdcard and pass that file's uri to the intent.
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 have trying to open XLSX file in android using intent filter to select existing XLSX reader application but always getting file not found exception. The same code works fine for XLS file. I added WRITE_EXTERNAL_STORAGE permission in manifest and file path being correct only. File also available in SD Card. My tablet have sheet to go and OfficeSuite application to read XLSX files. Even i can able to open my XLSX file by clicking on it. But trying to open via my application gives problem. For XLSX file my code always goes to else part. Thanks in advance.
private void openSpreadSheet(String fileCompletePath) {
File file = new File(fileCompletePath);
if (file.exists()) {
try {
Log.d(TAG, "File exist");
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/vnd.ms-excel");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
Toast.makeText(this,
"No application found to display spread sheet.",
Toast.LENGTH_SHORT).show();
}
} else {
Log.d(TAG, "File not exist");
}
}
If i remove if condition, then intent filter asked for choose application after choosing application, that selected application shows this error File not found(/Duplicate.xlsx:open failed:ENOENT(No such file or directory)).
Found Solution
Issue comes because I gave wrong file path.