【问题标题】:Issue with Android FileProvider passing URI to NotificationAndroid FileProvider 将 URI 传递给 Notification 的问题
【发布时间】:2016-10-11 12:26:00
【问题描述】:

我对 Android FileProvider 有一个奇怪的问题。我有文件由 Retrofit 2 下载并写下来做 InternalStorage(安全原因),然后我为这个文件创建 URI 并将其传递给 Intent。从这个意图我正在创建传递给通知的 PendingIntent。目标是通过单击通知打开文件。到目前为止一切顺利。

问题是使用下面列出的代码尝试几次我发生了SecurityException。现在在 logcat 中我看不到任何错误和异常,但文件似乎是空的(文件已正确下载 - 我通过相同的方法写入外部公共目录来检查它)。这是pdf文件,我的观点只是查看器中的文件名和黑色内容。

我用于创建 uri 和传递意图的代码:

        filesWebService.getFileFromUrl(url).subscribe((ResponseBody body) -> {
        String filename = URLUtil.guessFileName (url,null, null);
        Log.d("WebService","File received: " +filename);
        FileOutputStream fos = null;
        try {
            fos = openFileOutput(filename, Context.MODE_PRIVATE);
            InputStream inputStream = body.byteStream();
            byte[] buffer = new byte[1024];
            int bufferLength = 0;

            while((bufferLength = inputStream.read(buffer)) > 0) {
                fos.write(buffer, 0, bufferLength);
            }
            fos.close();
            File downloadedFile = new File(PdfDownloadService.this.getFilesDir(), filename);
            Log.d("DownloadService", downloadedFile.toString());
            Uri contentUri = FileProvider.getUriForFile(PdfDownloadService.this, "fantom.obtainpdf.fileprovider", downloadedFile);

            Log.d("DownloadService", contentUri.toString());
            Intent intent = new Intent(Intent.ACTION_VIEW, contentUri);
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            PendingIntent openFileIntent = PendingIntent.getActivity(PdfDownloadService.this, notif_id, intent , PendingIntent.FLAG_UPDATE_CURRENT);

            mNotificationUtil.cancelNotification(notif_id);

            mNotificationUtil.createNotificiationWithIntent(openFileIntent, "Pobrano plik", android.R.drawable.sym_def_app_icon, notif_id, true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    });

清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="fantom.obtainpdf">
...
<provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="fantom.obtainpdf.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
        <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
    </provider>

文件路径:

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

通知:

private NotificationCompat.Builder createNotification(String contentTitle, String contentText, @DrawableRes int icon, int notifID, boolean autoCancel){
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext)
            .setSmallIcon(icon)
            .setContentTitle(contentTitle)
            .setContentText(contentText)
            .setAutoCancel(autoCancel);
    return mBuilder;
}

public void createNotificiationWithIntent(PendingIntent intent, String contentTitle, @DrawableRes int icon, int notifID, boolean autoCancel){
    NotificationCompat.Builder builder = createNotification(contentTitle,"", icon, notifID, autoCancel);
    builder.setContentIntent(intent);
    mNotificationManager.notify(notifID, builder.build());
}

【问题讨论】:

    标签: android android-intent android-contentprovider android-fileprovider


    【解决方案1】:

    您的 FileProvider 未导出。 exported 标志为 false 时,其他应用无法访问它。

    【讨论】:

    • 谢谢,但正如我在问题中提到的 - 我出于安全原因将文件严格保密,因此导出设置为 true 会破坏 ContentProvider 的想法
    【解决方案2】:

    好的, 我设法发现了问题所在 - 我在 KitKat 上进行测试,并且存在需要解决方法的错误,如下所述:https://stackoverflow.com/a/32950381/2153648。添加这样的块后一切顺利。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多