【发布时间】:2019-12-23 08:01:49
【问题描述】:
我发现MediaStore.Images.Media.DATA已被弃用,所以我想用另一种方式获取图像真实路径。
这是我获取 uri 和图像标题的新方法,但我不知道如何获取真实路径。
我打印 uri、标题、路径:
photoUri: content://media/external/images/media/1605
标题:IMG_20191223143418
photoUri.getPath: /external/images/media/1605
但我想要这样:/storage/emulated/0/DCIM/1733/IMG_20191223143418.jpg
public static String getRealPathFromUri(Context context, Uri uri) throws IOException {
String filePath = null;
if (DocumentsContract.isDocumentUri(context, uri)) {
String documentId = DocumentsContract.getDocumentId(uri);
if (isMediaDocument(uri)) { // MediaProvider.
String id = documentId.split(":")[1];
String selection = MediaStore.Images.Media._ID + "=?";
String[] selectionArgs = {id};
filePath = getDataColumnImage(context, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection, selectionArgs);
} else if (isDownloadsDocument(uri)) { // DownloadsProvider.
Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(documentId));
filePath = getDataColumnImage(context, contentUri, null, null);
}
}
else if ("content".equalsIgnoreCase(uri.getScheme())) {
filePath = getDataColumnImage(context, uri, null, null);
}
else if ("file".equals(uri.getScheme())) {
filePath = uri.getPath();
}else
return filePath = null;
return filePath;
}
private static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
private static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
private static String getDataColumnImage(Context context, Uri uri, String selection, String[] selectionArgs) {
String path = null;
String[] projection = new String[]{
MediaStore.Files.FileColumns._ID,
MediaStore.Images.Media.DATE_TAKEN,
MediaStore.Images.Media.WIDTH,
MediaStore.Images.Media.HEIGHT,
MediaStore.MediaColumns.TITLE,
MediaStore.Images.Media.MIME_TYPE,
};
Cursor cursor = null;
try {
cursor = context.getContentResolver().query(
uri,
projection,
selection,
selectionArgs,
null
);
if (cursor != null && cursor.moveToFirst()) {
int idColumn = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns._ID);
int titleColumn = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.TITLE);
Uri photoUri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cursor.getString(idColumn));
String title = cursor.getString(titleColumn);
Log.e("TAG", "photoUri: " + photoUri);
Log.e("TAG", "title: " + title);
Log.e("TAG", "photoUri.getPath: " + photoUri.getPath());
}
} catch (Exception e) {
if (cursor != null) {
cursor.close();
}
}
return path;
// MediaStore.Images.Media.DATA is deprecated, so I want to use the other way as mentioned above to get real path.
/*String[] projection = new String[]{MediaStore.Images.Media.DATA};
Cursor cursor = null;
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
if (cursor != null && cursor.moveToFirst()) {
int columnIndex = cursor.getColumnIndexOrThrow(projection[0]);
path = cursor.getString(columnIndex);
}
} catch (Exception e) {
if (cursor != null) {
cursor.close();
}
}
return path;*/
}
【问题讨论】:
-
你不会再在 Q 上获得“真正的路径”。你甚至不会尝试它。好消息是您不需要“真正的路径”。直接使用获取的内容方案。
-
@blackapps 我需要真实的图片真实路径,因为我需要将图片上传到firebase,并且在上传之前我需要将所有图片转换为jpeg类型。
-
jpeg 类型与内容方案或文件方案无关。所以你是什么意思?对于上传,您可以直接使用内容方案。
-
@blackapps 我需要转换图像,因为用户可能会选择 png 类型的图像。而且我真的不知道你的内容方案是什么直接是什么意思,我尝试使用
content://com.android.providers.media.documents/document/image%3A1605之类的方式上传到firebase,它不起作用。 -
不是以'content://....'开头的吗?那是一个内容方案。您可以使用该方案将 .png 转换为 .jpg。很简单。
标签: android android-contentprovider mediastore android-10.0