Reputation: 212
I have strange issue with Android FileProvider. I have file downloaded by Retrofit 2 and wrriten down do InternalStorage(security reasons), then I am creating URI for this file and passing it to Intent. From this intent I am creating PendingIntent which is passed to Notification. Goal is to open file by clicking Notification. So far so good.
Issue is that trying few times with code listed below I occured SecurityException. Now in logcat I cannot see any error and exception, but file seems to be empty(file is downloaded correctly - I checked it by writing by the same method to external public dir). This is pdf file and my view is only filename in viewer and black content.
My code for creating uri and passing intents:
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:
<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:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="files"/>
</paths>
Notification:
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());
}
Upvotes: 0
Views: 1176
Reputation: 212
Ok, I managed to discover what is wrong - I was testing on KitKat, and there is bug which needs workaround as described here: https://stackoverflow.com/a/32950381/2153648 . After adding such block everything go smooth.
Upvotes: 0
Reputation: 522
Your FileProvider is not exported. Other apps cannot access it while the exported flag is false.
Upvotes: 1