how can I open a file that has been previously stored in the "privat" filesystem? The file is being downloaded by a webservice and should be stored on local fs. I got a "strange" error when trying to open the file via an Intent (Action_View). Although the file is present in the filesystem (saw the file in the file explorer in emulator/eclipse) it won't be shown in the calling galery activity that is launched. Instead of the picture the galery shows a black screen with not content in there (except the action bar). The same occurs when trying to open a pdf/movie/mp3 file via this method (pdf says for example that the file is corrupt).
BTW: The data that has been stored on the local fs is valid (not corrupt), I downloaded the files from debugger (via pull method) and the files can be opened on my workstation...
public void onAttachment(Context context, Attachment attachment) {
try {
//Attachment is a structured data object that contains of a byte[], filename and mimetype (like image/jpeg)
FileOutputStream fos = FileOutputStream fos = context.openFileOutput(attachment.getAttachmentFileName(), Context.MODE_PRIVATE);
fos.write(attachment.getBinary());
fos.flush();
fos.close();
File f = context.getFileStreamPath(attachment.getAttachmentFileName());
Uri uri = Uri.fromFile(f);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri,attachment.getAttachmentMimetype());
context.startActivity(intent);
} catch (IOException e) {
e.printStackTrace();
}
}
It is possible to start an app's activity by using Intent. setClassName according to the docs. To open it outside the current app, add this flag before starting the intent.
Intent intent = new Intent(Intent. ACTION_GET_CONTENT); intent. setType("*/*"); Intent i = Intent. createChooser(intent, "View Default File Manager"); startActivityForResult(i, CHOOSE_FILE_REQUESTCODE);
An intent allows you to start an activity in another app by describing a simple action you'd like to perform (such as "view a map" or "take a picture") in an Intent object.
What is the type of the Attachment in your code? Perhaps it returns wrong mime type?
This code works for me:
File file = new File("EXAMPLE.JPG");
// Just example, you should parse file name for extension
String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(".JPG");
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), mime);
startActivityForResult(intent, 10);
You cannot open files that are not on the sdcard like that. You'll need to copy the file to the sdcard and then opening it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With