在ACTION_VIEW缓存目录中打开文件 [英] Open file in cache directory with ACTION_VIEW

查看:216
本文介绍了在ACTION_VIEW缓存目录中打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找这种有一段时间了,但我不能让这样才能正常工作。让我来解释一下。

I have been searching for this for a while now but it I can't make this to work correctly. Let me explain.

我有一个Android应用程序,保存在高速缓存目录中的文件的(图像,文档,...)。起初,我用 getExternalCacheDir()方法,并将其保存在那里,而是因为它应该在没有SD卡的设备进行缓存,我必须使用 getCacheDir()

I have an android application that saves files (images, documents, ...) in the cache directory. At first I used to getExternalCacheDir() method and save them over there but because it should be cached on devices that do not have an SD-card, I have to use getCacheDir().

当我用 getExternalCacheDir()的方法,这是没有问题的,打开这样的其他应用程序的文件:

When I used to getExternalCacheDir() method, it was no problem to open those files in another application like this:

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), mimetype);

不过,使用时getCacheDir(),这些文件被保存在应用程序沙箱,并没有从应用程序外部访问。所以我GOOGLE了它,就在**的ContentProvider 。 ContentProvider的能够打开私人文件与外部应用程序。但是,当我尝试实现它,这是行不通的。

But when using the getCacheDir(), those files are saved in the application sandbox and are not accessible from outside the application. So I googled it and came on the **ContentProvider. A ContentProvider makes it possible to open private files with external applications. But when I try to implement it, it doesn't work.

我想实现的ContentProvider感谢这篇文章:<一个href="http://stackoverflow.com/questions/21304489/how-to-open-private-files-saved-to-the-internal-storage-using-intent-action-view">How打开保存在内部存储的私人文件使用Intent.ACTION_VIEW?,但没有成功。

I tried implementing the ContentProvider thanks to this post: How to open private files saved to the internal storage using Intent.ACTION_VIEW? but with no success.

package com.myapplication.providers;

import java.io.File;
import java.io.FileNotFoundException;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.ParcelFileDescriptor;

public class FileProvider extends ContentProvider {

    @Override
    public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
        File privateFile = new File(getContext().getCacheDir(), 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;
    }
}

添加此提供应用程序清单

Added this provider to the application manifest

<provider
    android:name="com.myapplication.providers.FileProvider"
    android:authorities="com.myapplication"
    android:exported="true" />

和使用这种code打开文件:

And using this code to open the file:

Uri uri = Uri.parse("content://com.myapplication/" + filename);

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(uri, mimetype);

在此先感谢!

Thanks in advance!

推荐答案

我发现我不得不做一些与众不同的事情。

I found out that I had to do things differently.

而不是创造我自己的ContentProvider时,V4支持库提供了可以使用FileProvider类。

Instead of creating my own ContentProvider, the v4 support library offers a FileProvider class that can be used.

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="be.myapplication"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths" />
</provider>

的FILE_PROVIDER_PATHS是描述哪些文件可以由其他应用程序读取一个XML文件。

The FILE_PROVIDER_PATHS is an xml file that describes which files can be read by other applications.

所以我增加了一个file_paths.xml文件到RES / XML文件夹。

So I added a file_paths.xml file to the res/xml folder.

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <cache-path name="my_cache" path="." />
</paths>

和你所要做的那么唯一的事情是创建和启动的意图,以显示该文件。

And the only thing you have to do then is create and start the intent to show the file.

File file = new File(getCacheDir(), "test.pdf");

Uri uri = FileProvider.getUriForFile(context, "be.myapplication", file);

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(uri, "application/pdf");

startActivity(intent);

而实际上就是这样。

And actually that's it.

这篇关于在ACTION_VIEW缓存目录中打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆