我有一个onActivityResult从一个mediastore图像选择返回,我可以获得一个图像使用以下URI:
Uri selectedImage = data.getData();
将this转换为字符串会得到:
content://media/external/images/media/47
或路径给出:
/external/images/media/47
然而,我似乎找不到一种方法将其转换为绝对路径,因为我想将图像加载到位图中,而不必复制到某个地方。我知道这可以使用URI和内容解析器来完成,但这似乎在重新启动手机时中断,我猜MediaStore在重新启动之间没有保持其编号相同。
试试这个
不过,如果你想要得到真正的答案,你可以试试我的答案。以上答案对我没有帮助。
解释:-这个方法获取URI,然后检查你的Android设备的API级别,根据API级别,它将生成真实路径。生成真实路径方法的代码根据API级别不同而不同。
method to get the Real path from URI
@SuppressLint("ObsoleteSdkInt")
public String getPathFromURI(Uri uri){
String realPath="";
// SDK < API11
if (Build.VERSION.SDK_INT < 11) {
String[] proj = { MediaStore.Images.Media.DATA };
@SuppressLint("Recycle") Cursor cursor = getContentResolver().query(uri, proj, null, null, null);
int column_index = 0;
String result="";
if (cursor != null) {
column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
realPath=cursor.getString(column_index);
}
}
// SDK >= 11 && SDK < 19
else if (Build.VERSION.SDK_INT < 19){
String[] proj = { MediaStore.Images.Media.DATA };
CursorLoader cursorLoader = new CursorLoader(this, uri, proj, null, null, null);
Cursor cursor = cursorLoader.loadInBackground();
if(cursor != null){
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
realPath = cursor.getString(column_index);
}
}
// SDK > 19 (Android 4.4)
else{
String wholeID = DocumentsContract.getDocumentId(uri);
// Split at colon, use second item in the array
String id = wholeID.split(":")[1];
String[] column = { MediaStore.Images.Media.DATA };
// where id is equal to
String sel = MediaStore.Images.Media._ID + "=?";
Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel, new String[]{ id }, null);
int columnIndex = 0;
if (cursor != null) {
columnIndex = cursor.getColumnIndex(column[0]);
if (cursor.moveToFirst()) {
realPath = cursor.getString(columnIndex);
}
cursor.close();
}
}
return realPath;
}
Use this method like this
Log.e(TAG, "getRealPathFromURI: "+getPathFromURI(your_selected_uri) );
输出:
04-06 12:39:46,993 6138-6138/com。qtm E/tag: getrealthfro到处都是:
/存储/ emulated / 0 /视频/ avengers_infinity_war_4k_8k-7680x4320。jpg
在过去的Android Q中,MediaStore.Images.Media.DATA将不再可用,有什么办法吗?这个字段在Android Q中被贬低了:
此常量在API级别29中已弃用。
应用程序可能没有直接访问此路径的文件系统权限。不要试图直接打开这个路径,应用程序应该使用ContentResolver#openFileDescriptor(Uri,字符串)来获得访问权限。
https://developer.android.com/reference/android/provider/MediaStore.MediaColumns.html#DATA
——编辑
据我所知,对于过去的Android Q,唯一的方法是依赖RELATIVE_PATH
该媒体项在存储设备中的相对路径。例如,存储在/storage/0000-0000/DCIM/Vacation/IMG1024.JPG的项的路径为DCIM/Vacation/。
https://developer.android.com/reference/android/provider/MediaStore.MediaColumns.html#RELATIVE_PATH
这个解决方案适用于所有情况:
在某些情况下,从URL中获取路径太难了。那你为什么需要路径?把文件复制到其他地方?你不需要路径。
public void SavePhotoUri (Uri imageuri, String Filename){
File FilePath = context.getDir(Environment.DIRECTORY_PICTURES,Context.MODE_PRIVATE);
try {
Bitmap selectedImage = MediaStore.Images.Media.getBitmap(context.getContentResolver(), imageuri);
String destinationImagePath = FilePath + "/" + Filename;
FileOutputStream destination = new FileOutputStream(destinationImagePath);
selectedImage.compress(Bitmap.CompressFormat.JPEG, 100, destination);
destination.close();
}
catch (Exception e) {
Log.e("error", e.toString());
}
}