java - 文件提供程序不工作

标签 java android

我想要做的是获取私有(private)区域中的文件以由另一个程序打开。在此示例中,是一个图像查看器应用程序。

AndroidManifest.xml

    <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.mydomain.myapp"
            android:exported="false"
            android:grantUriPermissions="true">
        <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/path" />
    </provider>

/res/xml/path.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <files-path name="cache" path="cache/"/>
</paths>

Java代码(文件位置)

File file = new File(context.getApplicationInfo().dataDir + File.separator + "cache" + File.separator + filename);

Java代码(打开文件)

    if(file != null && file.isFile() && file.canRead() && file.length() > 0){
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_SEND);

        List<ResolveInfo> resInfoList = this.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);

        Uri uri = Uri.parse(file.toURI().toString());
        for (int i = 0; i < resInfoList.size(); i++) {
            try{
                ResolveInfo resolveInfo = resInfoList.get(i);
                String packageName = resolveInfo.activityInfo.packageName;
                grantUriPermission(packageName, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
            }catch(Exception e){

            }catch(Error e){

            }
        }

        MimeTypeMap myMime = MimeTypeMap.getSingleton();
        Intent newIntent = new Intent(Intent.ACTION_VIEW);
        String mimeType = myMime.getMimeTypeFromExtension(".jpg");
        newIntent.setDataAndType(Uri.fromFile(file), mimeType);
        newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        try {
            context.startActivity(newIntent);
            return true;
        } catch (Exception e) {

        }
    }

这总是会导致一条 Toast 消息“无法找到项目”。我做错了什么以及如何解决这个问题?

最佳答案

What am I doing wrong

File file = new File(context.getApplicationInfo().dataDir + File.separator + "cache" + File.separator + filename);

使用 getCacheDir() 获取缓存目录,因为您的方法对于 Android 4.2+ 平板电脑和 Android 5.0+ 手机上的辅助帐户不可靠。所以,这应该是:

File file = new File(context.getCacheDir(), filename);

然后,您需要将 Uri.fromFile(file) 替换为对 FileProvider 上的 getUriForFile() 的调用,如the documentation 。您目前拥有的是指向文件的 Uri,而不是指向 ContentProvider,更不用说您的 FileProvider 实现了。

最后,在与 startActivity() 一起使用之前,您需要在 Intent 上调用 addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION),如下所示否则,响应 startActivity() 请求的应用将无法访问该内容。

This sample app演示了 FileProvider 的工作实现。

关于java - 文件提供程序不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33875742/

相关文章:

java - GcmListenerService.onMessageReceived() 未调用

android - "Multipart body must have at least one part"

android私有(private)属性突然变为空

Java stream groupingBy key作为最高薪水员工和值作为部门的所有员工

java Makefile 不起作用

java - 从文件中读取并分割行

java - XSLT 收集数据

java - Android Studio - 录音问题 - Java新手

Android java.exe 以非零退出值 1 结束

android - 将 OkHttp 与现有的 HttpUrlConnection 代码一起使用?