16

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();
    }
}           
Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
tim.kaufner
  • 1,247
  • 5
  • 13
  • 22
  • maybe it http://stackoverflow.com/questions/11068648/launching-an-intent-for-file-and-mime-type/11088980#11088980 can help. – ademar111190 Oct 05 '13 at 07:06

3 Answers3

12

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);
Ondřej Z
  • 5,094
  • 3
  • 24
  • 30
  • No, this won't work... The problem is that the activity is successfully being launched (galery, movie-/audio player, pdf-reader) but the content (file) seems invalid... – tim.kaufner Sep 25 '12 at 15:18
  • The attachment returns the correct mimetype, otherwise the correct activity (Galery/Mediaplayer/PDF-Reader) wouldn't be started at all. – tim.kaufner Sep 26 '12 at 06:48
  • Wrong. The parameter of getMimeTypeFromExtension MUST be a file extension without the leading '.'. – elmango Oct 30 '13 at 17:54
  • that worked for me. I was calling `Uri.parse(file.getPath())` instead of `Uri.fromFile(file)`. – IgniteCoders Dec 05 '16 at 08:08
2

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.

Givi
  • 3,283
  • 4
  • 20
  • 23
  • Can you give me a link where this is confirmed by google (in sdk documentation). I didn't find anything about that in the official documentation... BTW: When I open the file in an own activity that can handle images it works... – tim.kaufner Sep 26 '12 at 06:47
  • Unfortunately I'm talking out of experience so I don't have a link. BUT its the same problem as in uploading or emailing a file that's no in the SDCARD... Its the same with opening webview pages & etc.. Hope it helps... Sorry if not.. – Givi Sep 26 '12 at 10:09
  • So I have to write own Viewer-"Activities" that can handle the files that are stored on this private folder? – tim.kaufner Sep 26 '12 at 10:34
  • Or just copy them to a private folder on the SDCARD, and if you want delete them after. Private (actually invisible )folder on SDCARD: Environment.getExternalStorageDirectory() + "/.priavte_dir" – Givi Sep 26 '12 at 10:49
  • But the files will be accessible (by another application, for example a filebrowser-app) after that... Also you have you have to imply that the user has a sd-card and it must be mounted too. Hm... – tim.kaufner Sep 26 '12 at 11:37
2

When setting the location to "openFileOutput("fileName", Activity.MODE_WORLD_READABLE)" it will work. Other apps cannot read the data (even for viewing) stored when setting to "Context.MODE_PRIVATE"

see other post on stackoverflow

Community
  • 1
  • 1
tim.kaufner
  • 1,247
  • 5
  • 13
  • 22