Convert your "file: // ..." to the file path, find the identifier of the element with the following code, and then add it to the provider URI. Also, based on the file extensions, use the provider you want (for example, MediaStore.Video.Media.EXTERNAL_CONTENT_URI or MediaStore.Image.Media.EXTERNAL_CONTENT_URI)
public long getMediaItemIdFromProvider(Uri providerUri, Context appContext, String fileName) {
Sometimes MediaProvider is not updated immediately after adding one multimedia file to the device storage. You can force update your entries using this method:
private void refreshMediaProvider(Context appContext, String fileName) { MediaScannerConnection scanner = null; try { scanner = new MediaScannerConnection(appContext, null); scanner.connect(); try { Thread.sleep(200); } catch (Exception e) { } if (scanner.isConnected()) { Log.d(TAG_LOG, "Requesting scan for file " + fileName); scanner.scanFile(fileName, null); } } catch (Exception e) { Log.e(TAG_LOG, "Cannot to scan file", e); } finally { if (scanner != null) { scanner.disconnect(); } } }
source share