|
21 | 21 | import android.content.ContentUris; |
22 | 22 | import android.content.ContentValues; |
23 | 23 | import android.content.Context; |
24 | | -import android.content.Intent; |
| 24 | +import android.media.MediaScannerConnection; |
25 | 25 | import android.content.OperationApplicationException; |
26 | 26 | import android.database.Cursor; |
27 | 27 | import android.net.Uri; |
28 | | -import android.os.Build; |
29 | 28 | import android.os.RemoteException; |
30 | 29 | import android.provider.MediaStore; |
31 | 30 | import android.text.TextUtils; |
@@ -2015,37 +2014,28 @@ public List<OCShare> getSharesWithForAFile(String filePath, String accountName) |
2015 | 2014 | } |
2016 | 2015 |
|
2017 | 2016 | public static void triggerMediaScan(String path) { |
2018 | | - triggerMediaScan(path, null); |
| 2017 | + triggerMediaScan(MainApp.getAppContext(), path, null); |
2019 | 2018 | } |
2020 | 2019 |
|
2021 | 2020 | public static void triggerMediaScan(String path, OCFile file) { |
| 2021 | + triggerMediaScan(MainApp.getAppContext(), path, file); |
| 2022 | + } |
| 2023 | + |
| 2024 | + public static void triggerMediaScan(Context context, String path, OCFile file) { |
2022 | 2025 | if (path != null && !TextUtils.isEmpty(path)) { |
2023 | | - ContentValues values = new ContentValues(); |
2024 | | - ContentResolver contentResolver = MainApp.getAppContext().getContentResolver(); |
2025 | | - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) { |
2026 | | - if (Build.VERSION.SDK_INT == Build.VERSION_CODES.Q) { |
2027 | | - if (file != null) { |
2028 | | - values.put(MediaStore.Images.Media.MIME_TYPE, file.getMimeType()); |
2029 | | - values.put(MediaStore.Images.Media.TITLE, file.getFileName()); |
2030 | | - values.put(MediaStore.Images.Media.DISPLAY_NAME, file.getFileName()); |
2031 | | - } |
2032 | | - values.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis() / 1000); |
2033 | | - values.put(MediaStore.Images.Media.RELATIVE_PATH, path); |
2034 | | - values.put(MediaStore.Images.Media.IS_PENDING, 0); |
2035 | | - try { |
2036 | | - contentResolver.insert(MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY), |
2037 | | - values); |
2038 | | - } catch (IllegalArgumentException e) { |
2039 | | - Log_OC.e("MediaScanner", "Adding image to media scanner failed: " + e); |
| 2026 | + String mimeType = file != null ? file.getMimeType() : null; |
| 2027 | + MediaScannerConnection.scanFile( |
| 2028 | + context, |
| 2029 | + new String[]{path}, |
| 2030 | + mimeType != null ? new String[]{mimeType} : null, |
| 2031 | + (scannedPath, scannedUri) -> { |
| 2032 | + if (scannedUri != null) { |
| 2033 | + Log_OC.d(TAG, "Media scan completed for " + scannedPath); |
| 2034 | + } else { |
| 2035 | + Log_OC.w(TAG, "Media scan failed for " + scannedPath); |
2040 | 2036 | } |
2041 | | - } else { |
2042 | | - Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); |
2043 | | - intent.setData(Uri.fromFile(new File(path))); |
2044 | | - MainApp.getAppContext().sendBroadcast(intent); |
2045 | 2037 | } |
2046 | | - } else { |
2047 | | - Log_OC.d(TAG, "SDK > 29, skipping media scan"); |
2048 | | - } |
| 2038 | + ); |
2049 | 2039 | } |
2050 | 2040 | } |
2051 | 2041 |
|
|
0 commit comments