使用FileProvider在Android N打开下载的文件

问题描述 投票:11回答:5

我必须去解决我们的Android应用程序ň由于该FileProvider变化。我基本上都看过所有关于这个话题的最后我们的,但是没有找到解决办法为我所做的工作了。

下面是其开始从我们的应用程序下载,并将其存储于Download文件夹,并为ACTION_VIEW告诉他下载完成后调用一个DownloadManager意图作为soons我们之前的代码:

BroadcastReceiver onComplete = new BroadcastReceiver() {
    public void onReceive(Context ctxt, Intent intent) {
        Log.d(TAG, "Download commplete");

        // Check for our download
        long referenceId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
        if (mDownloadReference == referenceId) {
            DownloadManager.Query query = new DownloadManager.Query();
            query.setFilterById(mDownloadReference);
            Cursor c = mDownloadManager.query(query);
            if (c.moveToFirst()) {
                int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
                if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
                    String localUri = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
                    String fileExtension = MimeTypeMap.getFileExtensionFromUrl(localUri);
                    String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension);

                    if (mimeType != null) {
                        Intent openFileIntent = new Intent(Intent.ACTION_VIEW);
                        openFileIntent.setDataAndTypeAndNormalize(Uri.parse(localUri), mimeType);
                        openFileIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

                        try {
                            mAcme.startActivity(openFileIntent);
                        }
                        catch (ActivityNotFoundException e) {
                            // Ignore if no activity was found.
                        }
                    }
                }
            }
        }
    }
};

这适用于Android男,但打破了N个因流行FileUriExposedException。现在,我已经尝试使用FileProvider来解决这个问题,但我不能得到它的工作。它打破当我尝试获取内容URI:

Failed to find configured root that contains /file:/storage/emulated/0/Download/test.pdf

localUriDownloadManager返回的文件是:

file:///storage/emulated/0/Download/test.pdf

Environment.getExternalStorageDirectory()返回/storage/emulated/0,这是用于转换的代码:

File file = new File(localUri);
Log.d(TAG, localUri + " - " + Environment.getExternalStorageDirectory());
Uri contentUri = FileProvider.getUriForFile(ctxt, "my.file.provider", file);

AndroidManifest.xml

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

file_paths.xml

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

我已经尝试了所有值我能找到该XML文件。 :(

android android-fileprovider android-7.0-nougat
5个回答
© www.soinside.com 2019 - 2024. All rights reserved.