从android的文件path获取内容的uri

我知道一个图像的绝对path(比如,/sdcard/cats.jpg)。 有没有什么办法可以为这个文件的内容uri?

其实在我的代码我下载一个图像,并保存在一个特定的位置。 为了在ImageView中设置图像,我使用path打开文件,获取字节并创build一个位图,然后在ImageView中设置位图。 这是一个非常缓慢的过程,相反,如果我可以得到的内容uri,那么我可以很容易地使用方法ImageView.setImageUri(uri)

试试:

 ImageView.setImageURI(Uri.fromFile(new File("/sdcard/cats.jpg"))); 

或与:

 ImageView.setImageURI(Uri.parse(new File("/sdcard/cats.jpg").toString())); 

UPDATE

这里假定您的媒体(图像/video)已经添加到内容媒体提供商。 如果没有,那么你将无法得到你想要的内容URL。 相反会有文件Uri。

我有我的文件资源pipe理器活动相同的问题。 您应该知道文件的内容只支持图像,audio和video等媒体库数据。 我给你的代码获取图像内容的URI从SD卡中select一个图像。 试试这个代码,也许它会适合你…

 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)); cursor.close(); return Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "" + 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; } } } 

//此代码适用于2.2版本的图像,不知道是否有其他媒体types

  //Your file path - Example here is "/sdcard/cats.jpg" final String filePathThis = imagePaths.get(position).toString(); MediaScannerConnectionClient mediaScannerClient = new MediaScannerConnectionClient() { private MediaScannerConnection msc = null; { msc = new MediaScannerConnection(getApplicationContext(), this); msc.connect(); } public void onMediaScannerConnected(){ msc.scanFile(filePathThis, null); } public void onScanCompleted(String path, Uri uri) { //This is where you get your content uri Log.d(TAG, uri.toString()); msc.disconnect(); } }; 

接受的解决scheme可能是您的目的最好的select,但实际上在主题行中回答这个问题:

在我的应用程序中,我必须从URI获取path并从path获取URI。 前者:

 /** * Gets the corresponding path to a file from the given content:// URI * @param selectedVideoUri The content:// URI to find the file path from * @param contentResolver The content resolver to use to perform the query. * @return the file path as a string */ private String getFilePathFromContentUri(Uri selectedVideoUri, ContentResolver contentResolver) { String filePath; String[] filePathColumn = {MediaColumns.DATA}; Cursor cursor = contentResolver.query(selectedVideoUri, filePathColumn, null, null, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); filePath = cursor.getString(columnIndex); cursor.close(); return filePath; } 

后者(我为video做的,但也可以通过MediaStore.Audio(等)代替MediaStore.Video用于audio或文件或其他types的存储内容):

 /** * Gets the MediaStore video ID of a given file on external storage * @param filePath The path (on external storage) of the file to resolve the ID of * @param contentResolver The content resolver to use to perform the query. * @return the video ID as a long */ private long getVideoIdFromFilePath(String filePath, ContentResolver contentResolver) { long videoId; Log.d(TAG,"Loading file " + filePath); // This returns us content://media/external/videos/media (or something like that) // I pass in "external" because that's the MediaStore's name for the external // storage on my device (the other possibility is "internal") Uri videosUri = MediaStore.Video.Media.getContentUri("external"); Log.d(TAG,"videosUri = " + videosUri.toString()); String[] projection = {MediaStore.Video.VideoColumns._ID}; // TODO This will break if we have no matching item in the MediaStore. Cursor cursor = contentResolver.query(videosUri, projection, MediaStore.Video.VideoColumns.DATA + " LIKE ?", new String[] { filePath }, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(projection[0]); videoId = cursor.getLong(columnIndex); Log.d(TAG,"Video ID is " + videoId); cursor.close(); return videoId; } 

基本上, MediaStoreDATA列(或者你要查询的任何子节)存储文件path,所以你可以使用这个信息来查找它。

只需使用adb shell CLI命令即可获得文件ID而无需编写任何代码:

 adb shell content query --uri "content://media/external/video/media" | grep FILE_NAME | grep -Eo " _id=([0-9]+)," | grep -Eo "[0-9]+" 

你可以尝试下面的代码片段

  public Uri getUri(ContentResolver cr, String path){ Uri mediaUri = MediaStore.Files.getContentUri(VOLUME_NAME); Cursor ca = cr.query(mediaUri, new String[] { MediaStore.MediaColumns._ID }, MediaStore.MediaColumns.DATA + "=?", new String[] {path}, null); if (ca != null && ca.moveToFirst()) { int id = ca.getInt(ca.getColumnIndex(MediaStore.MediaColumns._ID)); ca.close(); return MediaStore.Files.getContentUri(VOLUME_NAME,id); } ca.close(); return null; } 

最容易和创build内容Uri content://的强大方式content://从文件是使用FileProvider 。 Uri提供的FileProvider也可以用来提供Uri与其他应用程序共享文件。 要从File的绝对path获取文件Uri,您可以使用DocumentFile.fromFile(new File(path,name)),它将在Api 22中添加,并在以下版本中返回null。

 File imagePath = new File(Context.getFilesDir(), "images"); File newFile = new File(imagePath, "default_image.jpg"); Uri contentUri = getUriForFile(getContext(), "com.mydomain.fileprovider", newFile);