Browse Source

Merge pull request #5969 from nextcloud/mediaScanner

Replace media scanner on newer Android versions
Tobias Kaminsky 5 years ago
parent
commit
eae71a31e4

+ 23 - 4
src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java

@@ -31,6 +31,7 @@ import android.content.Intent;
 import android.content.OperationApplicationException;
 import android.database.Cursor;
 import android.net.Uri;
+import android.os.Build;
 import android.os.RemoteException;
 import android.provider.MediaStore;
 import android.text.TextUtils;
@@ -395,7 +396,7 @@ public class FileDataStorageManager {
                     if (file.isDown()) {
                         String path = file.getStoragePath();
                         if (new File(path).delete() && MimeTypeUtil.isMedia(file.getMimeType())) {
-                            triggerMediaScan(path); // notify MediaScanner about removed file
+                            triggerMediaScan(path, file); // notify MediaScanner about removed file
                         }
                     }
                 }
@@ -1750,10 +1751,28 @@ public class FileDataStorageManager {
     }
 
     public static void triggerMediaScan(String path) {
+        triggerMediaScan(path, null);
+    }
+
+    public static void triggerMediaScan(String path, OCFile file) {
         if (path != null) {
-            Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
-            intent.setData(Uri.fromFile(new File(path)));
-            MainApp.getAppContext().sendBroadcast(intent);
+            ContentValues values = new ContentValues();
+            ContentResolver contentResolver = MainApp.getAppContext().getContentResolver();
+            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
+                if (file != null) {
+                    values.put(MediaStore.Images.Media.MIME_TYPE, file.getMimeType());
+                    values.put(MediaStore.Images.Media.TITLE, file.getFileName());
+                    values.put(MediaStore.Images.Media.DISPLAY_NAME, file.getFileName());
+                }
+                values.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis() / 1000);
+                values.put(MediaStore.Images.Media.RELATIVE_PATH, path);
+                values.put(MediaStore.Images.Media.IS_PENDING, 0);
+                contentResolver.insert(MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY), values);
+            } else {
+                Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
+                intent.setData(Uri.fromFile(new File(path)));
+                MainApp.getAppContext().sendBroadcast(intent);
+            }
         }
     }
 

+ 1 - 1
src/main/java/com/owncloud/android/files/services/FileDownloader.java

@@ -517,7 +517,7 @@ public class FileDownloader extends Service
         file.setRemoteId(mCurrentDownload.getFile().getRemoteId());
         mStorageManager.saveFile(file);
         if (MimeTypeUtil.isMedia(mCurrentDownload.getMimeType())) {
-            FileDataStorageManager.triggerMediaScan(file.getStoragePath());
+            FileDataStorageManager.triggerMediaScan(file.getStoragePath(), file);
         }
         mStorageManager.saveConflict(file, null);
     }

+ 1 - 1
src/main/java/com/owncloud/android/operations/RenameFileOperation.java

@@ -134,7 +134,7 @@ public class RenameFileOperation extends SyncOperation {
                 getStorageManager().deleteFileInMediaScan(oldPath);
                 // notify to scan about new file, if it is a media file
                 if (MimeTypeUtil.isMedia(file.getMimeType())) {
-                    FileDataStorageManager.triggerMediaScan(newPath);
+                    FileDataStorageManager.triggerMediaScan(newPath, file);
                 }
             }
             // else - NOTHING: the link to the local file is kept although the local name

+ 1 - 1
src/main/java/com/owncloud/android/operations/UploadFileOperation.java

@@ -1305,7 +1305,7 @@ public class UploadFileOperation extends SyncOperation {
         getStorageManager().saveConflict(file, null);
 
         if (MimeTypeUtil.isMedia(file.getMimeType())) {
-            FileDataStorageManager.triggerMediaScan(file.getStoragePath());
+            FileDataStorageManager.triggerMediaScan(file.getStoragePath(), file);
         }
 
         // generate new Thumbnail