I have a WebView that is downloading a ZIP file to the device and displaying a notification to the user. My emulator has a Files app that can open the file, but the user should be able to tap the notification and open the file. Ideally, the Files app would open it since it can handle ZIP files, but if there is nothing that can open it, I'd like the user to be given a link to the Play store to download something that can.
Here is the code that builds the notification:
Intent intent = SSLauncher.getOpenFileIntent(context, dwldsPath);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder b = new NotificationCompat.Builder(context, "4565")
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.stat_sys_download_anim0)
.setContentTitle("File Download")
.setContentText(dwldsPath.getName())
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
nm = (NotificationManager) this.context.getSystemService(Context.NOTIFICATION_SERVICE);
if(nm != null) {
nm.notify(notificationId, b.build());
}
I've tried two Intents. This one does nothing:
Uri uri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".fileprovider", file);
MimeTypeMap mime = MimeTypeMap.getSingleton();
String type = mime.getMimeTypeFromExtension("zip");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(uri, type);
return intent;
And this one give me a message saying "No app can perform this action:"
String type = "application/zip";
Uri uri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".fileprovider", file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(uri, type);
Intent chooser = Intent.createChooser(intent, "open file with");
return chooser;