0

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());
}
4

2 に答える 2

1

FileProvider はエクスポートされません。exportフラグが falseの間、他のアプリはそれにアクセスできません。

于 2016-06-14T07:42:33.033 に答える