简体   繁体   English

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

[英]How to open a file in Android via an Intent

how can I open a file that has been previously stored in the "privat" filesystem? 如何打开以前存储在“privat”文件系统中的文件? The file is being downloaded by a webservice and should be stored on local fs. 该文件由Web服务下载,应存储在本地fs中。 I got a "strange" error when trying to open the file via an Intent (Action_View). 尝试通过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. 虽然文件存在于文件系统中(在模拟器/ eclipse中看到文件浏览器中的文件),但它不会显示在启动的调用galery活动中。 Instead of the picture the galery shows a black screen with not content in there (except the action bar). 而不是图片,galery显示黑色屏幕没有内容(除了操作栏)。 The same occurs when trying to open a pdf/movie/mp3 file via this method (pdf says for example that the file is corrupt). 尝试通过此方法打开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... BTW:存储在本地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();
    }
}           

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

You cannot open files that are not on the sdcard like that. 你无法打开那些不在SD卡上的文件。 You'll need to copy the file to the sdcard and then opening it. 您需要将文件复制到SD卡然后打开它。

When setting the location to " openFileOutput("fileName", Activity.MODE_WORLD_READABLE) " it will work. 将位置设置为“ openFileOutput("fileName", Activity.MODE_WORLD_READABLE) ,它将起作用。 Other apps cannot read the data (even for viewing) stored when setting to " Context.MODE_PRIVATE " 设置为“ Context.MODE_PRIVATE ”时,其他应用无法读取存储的数据(即使是查看)

see other post on stackoverflow 请参阅stackoverflow上的其他帖子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM