我对Android FileProvider有一个奇怪的问题。我有通过Retrofit 2下载的文件并且写下来执行InternalStorage(安全原因),然后我为这个文件创建URI并将其传递给Intent。从这个意图我创建PendingIntent,传递给Notification。目标是通过单击通知打开文件。到目前为止一切都很好。
问题是尝试几次使用下面列出的代码我发生了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());
}
答案 0 :(得分:1)
您的FileProvider未导出。 导出的标志为false时,其他应用无法访问它。
答案 1 :(得分:0)
确定, 我设法发现了什么问题 - 我正在KitKat上进行测试,并且存在需要解决方法的bug,如下所述:https://stackoverflow.com/a/32950381/2153648。添加这样的块后,一切顺利。