如何通过 Intent 在 Android 中打开文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12585747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:07:03  来源:igfitidea点击:

How to open a file in Android via an Intent

androidandroid-intentfilesystemsactionview

提问by tim.kaufner

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).

如何打开以前存储在“privat”文件系统中的文件?该文件正在由网络服务下载,应存储在本地文件系统中。尝试通过 Intent (Action_View) 打开文件时出现“奇怪”错误。尽管该文件存在于文件系统中(在 emulator/eclipse 的文件资源管理器中看到该文件),但它不会显示在启动的调用 galery 活动中。galery 显示的不是图片,而是黑屏,其中没有内容(操作栏除外)。尝试通过此方法打开 pdf/movie/mp3 文件时也会发生同样的情况(例如,pdf 表示文件已损坏)。

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...

顺便说一句:存储在本地 fs 上的数据是有效的(未损坏),我从调试器下载了文件(通过 pull 方法),文件可以在我的工作站上打开......

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();
    }
}           

采纳答案by tim.kaufner

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"

将位置设置为“ openFileOutput("fileName", Activity.MODE_WORLD_READABLE)”时,它将起作用。其他应用程序无法读取设置为“ Context.MODE_PRIVATE”时存储的数据(即使是查看)

see other post on stackoverflow

请参阅有关 stackoverflow 的其他帖子

回答by Ond?ej Z

What is the type of the Attachment in your code? Perhaps it returns wrong mime type?

您的代码中的附件类型是什么?也许它返回错误的 MIME 类型?

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);

回答by Givi

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.

您不能像那样打开不在 sdcard 上的文件。您需要将文件复制到 SD 卡,然后打开它。