如何通过一个Intent打开Android的文件文件、Intent、Android

2023-09-07 14:16:15 作者:童话、只是童年的笑话

我怎么能打开已pviously存储在普里瓦文件系统$ P $文件?该文件是由一个web服务下载和应存储在本地fs的。我想通过一个Intent(ACTION_VIEW)打开文件时,一个奇怪的错误。尽管文件是在文件系统present(看到在模拟器/月食文件浏览器中的文件),它不会在启动呼叫galery活动显示。取而代之的是画面的显示galery一个黑色的屏幕并不满足在那里(除了操作栏)。尝试通过这个方法(PDF举例说,该文件已损坏),打开一个PDF /电影/ MP3文件时同样会发生。

BTW:已存储在本地FS中的数据是有效的(未损坏),我下载的调试器(通过拉法)的文件,这些文件可以在工作站上的...

打开

 公共无效onAttachment(上下文的背景下,附件附件){    尝试{        //附件是一个包含一个字节的结构化数据对象[],文件名和MIME类型(如图像/ JPEG)        FOS的FileOutputStream = FileOutputStream中FOS = context.openFileOutput(attachment.getAttachmentFileName(),Context.MODE_PRIVATE);        fos.write(attachment.getBinary());        fos.flush();        fos.close();        文件f = context.getFileStreamPath(attachment.getAttachmentFileName());        URI URI = Uri.fromFile(F);        意向意图=新意图(Intent.ACTION_VIEW);        intent.setDataAndType(URI,attachment.getAttachmentMimetype());        context.startActivity(意向);    }赶上(IOException异常五){        e.printStackTrace();    }} 

解决方案

在设定的位置 openFileOutput(文件名,Activity.MODE_WORLD_READABLE),将工作。其他应用无法读取(即使是观看)设置为当存储 Context.MODE_PRIVATE

数据

看other张贴在计算器

Android学习日记 一

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

解决方案

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