如何使用Intent.ACTION_VIEW打开保存到内部存储的私有文件?

时间:2014-01-23 09:45:46

标签: android android-intent

我正在尝试一个示例程序将文件存储在内部存储中并使用它打开它 Intent.ACTION_VIEW。

为了以私密模式存储文件,我按照提供的步骤here.

进行操作

我能够在/data/data/com.storeInternal.poc/files的内部存储中找到创建的文件。*

  

但是当我试图打开文件时,它无法打开。

请在下面找到我用过的代码。

public class InternalStoragePOCActivity extends Activity {
    /** Called when the activity is first created. */
    String FILENAME = "hello_file.txt";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        createFile();
        openFile(FILENAME);
    }

    public FileOutputStream getStream(String path) throws FileNotFoundException {
        return openFileOutput(path, Context.MODE_PRIVATE);
    }

    public void createFile(){

        String string = "hello world!";
        FileOutputStream fout = null;
        try {
            //getting output stream
            fout = getStream(FILENAME);
            //writng data
            fout.write(string.getBytes());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(fout!=null){
                //closing the output stream
                try {
                    fout.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }

    public void openFile(String filePath) {
        try {
            File temp_file = new File(filePath);

            Uri data = Uri.fromFile(temp_file);
            String type = getMimeType(data.toString());

            Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
            intent.setDataAndType(data, type);
            startActivity(intent);

        } catch (Exception e) {
            Log.d("Internal Storage POC ", "No Supported Application found to open this file");
            e.printStackTrace();

        }
    }

    public static String getMimeType(String url) {
        String type = null;
        String extension = MimeTypeMap.getFileExtensionFromUrl(url);

        if (extension != null) {
            MimeTypeMap mime = MimeTypeMap.getSingleton();
            type = mime.getMimeTypeFromExtension(extension);
        }

        return type;
    }
}

如何通过任何其他现有/适当的应用程序打开使用 Context.MODE_PRIVATE 存储的文件。例如:file.pdf应该由PDF阅读器,视频播放器等打开。

3 个答案:

答案 0 :(得分:50)

您无法通过Intent将内部存储中的文件(由URI引用)共享/发送到另一个应用程序。应用无法读取其他应用的私有数据(除非是通过内容提供商)。您在intent(而不是实际文件本身)中传递文件的URI,并且接收intent的应用程序需要能够从该URI读取。

最简单的解决方案是先将文件复制到外部存储,然后从那里共享。如果您不想这样做,请创建一个Content Provider来公开您的文件。可以在此处找到一个示例:Create and Share a File from Internal Storage

编辑:使用内容提供商:

首先创建您的内容提供程序类,如下所示。这里重要的覆盖是'openFile'。使用文件URI调用内容提供程序时,此方法将运行并为其返回ParcelFileDescriptor。其他方法也需要存在,因为它们在ContentProvider中是抽象的。

public class MyProvider extends ContentProvider {

@Override
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
    File privateFile = new File(getContext().getFilesDir(), uri.getPath());
    return ParcelFileDescriptor.open(privateFile, ParcelFileDescriptor.MODE_READ_ONLY);
}

@Override
public int delete(Uri arg0, String arg1, String[] arg2) {
    return 0;
}

@Override
public String getType(Uri arg0) {
    return null;
}

@Override
public Uri insert(Uri arg0, ContentValues arg1) {
    return null;
}

@Override
public boolean onCreate() {
    return false;
}

@Override
public Cursor query(Uri arg0, String[] arg1, String arg2, String[] arg3,
        String arg4) {
    return null;
}

@Override
public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) {
    return 0;
}
}

在应用程序代码中的Manifest中定义您的提供程序,exports = true以允许其他应用程序使用它:

<provider android:name=".MyProvider" android:authorities="your.package.name" android:exported="true" />

在Activity中的openFile()方法中,设置如下的URI。此URI指向包中的内容提供程序以及文件名:

Uri uri = Uri.parse("content://your.package.name/" + filePath);

最后,在意图中设置此uri:

intent.setDataAndType(uri, type);

请记住在我上面使用'your.package.name'的地方插入您自己的包名。

答案 1 :(得分:1)

如果有人仍然面临以下问题:无法访问该文件。这可能对你有帮助。与内容提供商一起,您还必须设置以下标志...

intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

intent.setFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);

答案 2 :(得分:0)

 File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
        + "Foder path" + "/" + "File Name");

        Uri uri = Uri.fromFile(file);
        Intent in = new Intent(Intent.ACTION_VIEW);
        in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        in.setDataAndType(uri, "mime type");
        startActivity(in);

[注意:请根据您的文件类型选择mime类型]