2017-11-04 7 views
0

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; 
    } 

Antwort

0

Ich sehe zwei Probleme, die Ihre Probleme beitragen können.

Zuerst verwenden Sie implizite Broadcasts (Aktionszeichenfolgen anstelle von Komponentennamen) zu Ihrer eigenen App. Aus Sicherheitsgründen war das keine besonders gute Idee, und implizite Broadcasts sind im Allgemeinen über Android 8.0+ verboten. Ersetzen Sie also new Intent(ACTION_OPEN_FILE) durch new Intent(this, WhateverTheReceiverClassIs.class).setAction(ACTION_OPEN_FILE), wobei WhateverTheReceiverClassIs unabhängig von Ihrer BroadcastReceiver Klasse ist.

Zweitens wird canOpenFile()Uri.fromFile() verwenden, können Sie ein file Schema auf dem Uri geben. Da das file-Schema im Allgemeinen auf Android 7.0 verboten wurde, ist es durchaus möglich, dass es in Ihrer Android 8.0-Testumgebung keine <intent-filter> dafür gibt. Außerdem stimmt dieser Code nicht mitüberein. Erzeuge die Uri einmal und verwende sie für beide Szenarien.

Verwandte Themen