首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android FileProvider将URI传递给通知时的问题

Android FileProvider将URI传递给通知时的问题
EN

Stack Overflow用户
提问于 2016-06-10 13:44:38
回答 1查看 1.1K关注 0票数 0

我对Android FileProvider有个奇怪的问题。我已经下载了Retrofit 2下载的文件并下载了do InternalStorage(安全原因),然后我将为该文件创建URI并将其传递给意向。出于这个目的,我创建了传递给通知的PendingIntent。目标是通过单击“通知”打开文件。到目前一切尚好。

问题是,使用下面列出的代码尝试了几次,我发生了SecurityException。现在,在logcat中,我看不到任何错误和异常,但是文件似乎是空的(文件下载正确--我用相同的方法将文件写入外部public dir进行了检查)。这是pdf文件,我的视图只是在查看器和黑色内容中的文件名。

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

代码语言:javascript
复制
        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();
        }
    });

清单:

代码语言:javascript
复制
<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>

FilePaths:

代码语言:javascript
复制
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="files"/>
</paths>

通知:

代码语言:javascript
复制
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());
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-14 08:56:33

好的,我设法发现了问题所在--我在KitKat上进行了测试,并且有一些bug需要解决,如这里所描述的:https://stackoverflow.com/a/32950381/2153648。在加了这样的块后,一切都变得平滑了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37749873

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档