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