Get Uri from the Real Way

I have a real path to a file like "file: ///mnt/sdcard/3dphoto/temp19.jps", how can I get uri like "content: // media / external / images / media / 1"?

+6
source share
3 answers

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)

/** * Given a media filename, returns it id in the media content provider * * @param providerUri * @param appContext * @param fileName * @return */ public long getMediaItemIdFromProvider(Uri providerUri, Context appContext, String fileName) { //find id of the media provider item based on filename String[] projection = { MediaColumns._ID, MediaColumns.DATA }; Cursor cursor = appContext.getContentResolver().query( providerUri, projection, MediaColumns.DATA + "=?", new String[] { fileName }, null); if (null == cursor) { Log.d(TAG_LOG, "Null cursor for file " + fileName); return ITEMID_NOT_FOUND; } long id = ITEMID_NOT_FOUND; if (cursor.getCount() > 0) { cursor.moveToFirst(); id = cursor.getLong(cursor.getColumnIndexOrThrow(BaseColumns._ID)); } cursor.close(); return id; } 

Sometimes MediaProvider is not updated immediately after adding one multimedia file to the device storage. You can force update your entries using this method:

 /** * Force a refresh of media content provider for specific item * * @param fileName */ 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(); } } } 
+6
source

I had the same question for my activity in the file explorer ... but it should turn out that the content content for the file only supports media such as image, audio and video .... I give you the opportunity to get the URL image address, image from sdcard .... try this code ... maybe this will work for you ...

 public static Uri getImageContentUri(Context context, File imageFile) { String filePath = imageFile.getAbsolutePath(); Cursor cursor = context.getContentResolver().query( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[] { MediaStore.Images.Media._ID }, MediaStore.Images.Media.DATA + "=? ", new String[] { filePath }, null); if (cursor != null && cursor.moveToFirst()) { int id = cursor.getInt(cursor .getColumnIndex(MediaStore.MediaColumns._ID)); Uri baseUri = Uri.parse("content://media/external/images/media"); return Uri.withAppendedPath(baseUri, "" + id); } else { if (imageFile.exists()) { ContentValues values = new ContentValues(); values.put(MediaStore.Images.Media.DATA, filePath); return context.getContentResolver().insert( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); } else { return null; } } } 
+6
source

Another easy way:

 File file = new File(Path); Uri uri = Uri.fromFile(file); 
+1
source

Source: https://habr.com/ru/post/900376/


All Articles