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