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");
Related
When I need to open a file with SAF (from storage or from DocumentsProvider) I just call Intent :
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType(TYPE_ALL);
try {
startActivityForResult(intent, OPEN_REQUEST_CODE);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(this, R.string.excaption_not_found_open,
Toast.LENGTH_SHORT).show();
}
When I need to delete a FILE I do just the same, except OPEN_REQUEST_CODE - I change it with DELETE_REQUEST_CODE, get URI and call Delete() method.
The problem is I don't know how to delete DIRECTORY. I have a method that does it pragmatically, but I can't figure how to use it with standard file viewer - it only OPENS directory - there is no Intent to delete it...
I guess I have to write my own File Manager to do that. Is there any other way?
Check this from the Android Developer documentation:
How do we code to get the "Open from" dialog, on an Android device?
I can find many source code examples for reading a csv file, especially when located in the res\raw directory, but I could find no example of how to have an Android app issue a dialog to read the Downloads directory and select a csv file stored in it.
Thanks a lot!!!
I found hints to the solution in an article entitled Android Import excel into Sqlite Database Part 1
To get that dialog:
Intent fileIntent = new Intent(Intent.ACTION_GET_CONTENT);
fileIntent.setType("text/csv")
try {
startActivityForResult(fileIntent, REQUEST_CODE);
} catch (ActivityNotFoundException e) {
Toast.makeText(MainActivity.this, "No app found for importing the file.", Toast.LENGTH_LONG).show();
}
where REQUEST_CODE is 1.
You can then later on access the selected file by:
String filepath = data.getData().getPath();
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.
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);