Ich habe Probleme mit dem Öffnen von Dateien auf Android O (API 26).Datei öffnen in Android O funktioniert nicht, aber erfolgreich auf API 25
In Android O, wenn ich auf die Registerkarte Benachrichtigung nach erfolgreicher Download, passiert nichts jedoch (Meldung wird nur gelöscht), wenn ich in das gleiche tun 25 in api die Datei als öffnet es und es funktioniert sein sollte.
Debugging weist darauf hin, das die Methode canOpenFile gibt immer false zurück auf api 26.
Habe ich etwas übersehen? Jede Hilfe wird geschätzt, viele thx.
Benachrichtigungscode:
public void finishedNotification(int id, String appName, String urlPath, boolean success) {
initSoundChannels(this);
NotificationCompat.Builder notification = new NotificationCompat.Builder(this, "default");
notification.setContentTitle(appName)
.setContentText(success ? "Successfully downloaded" : "Download failed - file deleted")
.setSmallIcon(R.mipmap.ic_noti)
.setWhen(System.currentTimeMillis())
.setColor(mApplication_.getInstance().getResources().getColor(R.color.apps_color))
.setLargeIcon(BitmapFactory.decodeResource(mApplication_.getInstance().getResources(), R.mipmap.ic_launcher))
.setOngoing(false)
.setPriority(Notification.PRIORITY_HIGH)
.setAutoCancel(true);
if (success) {
if (canOpenFile(urlPath)) { // ALWAYS false on API26??
PendingIntent openFile = PendingIntent.getActivity(getApplicationContext(),
ACTION_OPEN_FILE.hashCode() + id,
openFile(urlPath),
PendingIntent.FLAG_CANCEL_CURRENT);
notification.setContentIntent(openFile);
} else { // being executed on api 26
PendingIntent openDownloads = PendingIntent.getBroadcast(getApplicationContext(),
ACTION_OPEN_DOWNLOADS.hashCode() + id,
new Intent(ACTION_OPEN_FILE),
PendingIntent.FLAG_CANCEL_CURRENT);
notification.setContentIntent(openDownloads);
}
} else {
Intent cancelIntent = new Intent(ACTION_FILE_CANCEL);
cancelIntent.putExtra("id", id);
PendingIntent cancel = PendingIntent.getBroadcast(mApplication_.getInstance(),
ACTION_FILE_CANCEL.hashCode() + id,
cancelIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
notification.setContentIntent(cancel);
}
Notification builtNotification = notification.build();
getNotificationManager().notify(id, builtNotification);
}
Openfile Intent:
private Intent openFile(String urlPath) {
File file = new File(urlPath);
Intent target = new Intent(Intent.ACTION_VIEW);
MimeTypeMap mime = MimeTypeMap.getSingleton();
String ext = file.getName().substring(file.getName().lastIndexOf(".") + 1);
String type = mime.getMimeTypeFromExtension(ext);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri uri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider", file);
target.setDataAndType(uri, type);
target.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else {
Uri uri = Uri.fromFile(file);
target.setDataAndType(uri, type);
target.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
return target;
}
CanOpenfile:
private boolean canOpenFile(String urlPath) {
PackageManager pm = getPackageManager();
File file = new File(urlPath);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.fromFile(file));
ResolveInfo info = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
return info != null;
}