[英]How to get file path of image from URI in Android Lollipop?
I want to create an image picker for Android Lollipop. 我想为Android Lollipop创建一个图像选择器。 I use
Intent.ACTION_GET_CONTENT
to choose my picture but I cannot get the path of selected image from URI, which the dialog picker returned. 我使用
Intent.ACTION_GET_CONTENT
来选择我的图片,但是我无法从URI中获取所选图像的路径,对话框选择器返回了该路径。
I tried codes from these threads: 我试过这些主题的代码:
retrieve absolute path when select image from gallery kitkat android 从gallery kitkat android中选择图像时检索绝对路径
https://chintankhetiya.wordpress.com/2013/12/14/picture-selection-from-camera-gallery/ https://chintankhetiya.wordpress.com/2013/12/14/picture-selection-from-camera-gallery/
but all of them return null. 但是所有这些都返回null。 Can any one help me?
谁能帮我?
Try 尝试
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.CursorLoader;
import android.database.Cursor;
import android.net.Uri;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
public class RealPathUtil {
@SuppressLint("NewApi")
public static String getRealPathFromURI_API19(Context context, Uri uri) {
Log.variable("uri", uri.getPath());
String filePath = "";
if (DocumentsContract.isDocumentUri(context, uri)) {
String wholeID = DocumentsContract.getDocumentId(uri);
Log.variable("wholeID", wholeID);
// Split at colon, use second item in the array
String[] splits = wholeID.split(":");
if (splits.length == 2) {
String id = splits[1];
String[] column = {MediaStore.Images.Media.DATA};
// where id is equal to
String sel = MediaStore.Images.Media._ID + "=?";
Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
column, sel, new String[]{id}, null);
int columnIndex = cursor.getColumnIndex(column[0]);
if (cursor.moveToFirst()) {
filePath = cursor.getString(columnIndex);
}
cursor.close();
}
} else {
filePath = uri.getPath();
}
return filePath;
}
@SuppressLint("NewApi")
public static String getRealPathFromURI_API11to18(Context context, Uri contentUri) {
String[] proj = {MediaStore.Images.Media.DATA};
String result = null;
CursorLoader cursorLoader = new CursorLoader(
context,
contentUri, proj, null, null, null);
Cursor cursor = cursorLoader.loadInBackground();
if (cursor != null) {
int column_index =
cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
result = cursor.getString(column_index);
}
return result;
}
public static String getRealPathFromURI_BelowAPI11(Context context, Uri contentUri) {
String[] proj = {MediaStore.Images.Media.DATA};
Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index
= cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
and use: 并使用:
private String uriToFilename(Uri uri) {
String path = null;
if (Build.VERSION.SDK_INT < 11) {
path = RealPathUtil.getRealPathFromURI_BelowAPI11(mContext, uri);
} else if (Build.VERSION.SDK_INT < 19) {
path = RealPathUtil.getRealPathFromURI_API11to18(mContext, uri);
} else {
path = RealPathUtil.getRealPathFromURI_API19(mContext, uri);
}
return path;
}
works for me in must cases ( not works for download app) 在必须的情况下适合我( 不适用于下载应用程序)
Try this little function: 尝试这个小功能:
public static String getRealPathFromUri(Uri uri) {
String result = "";
String documentID;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
String[] pathParts = uri.getPath().split("/");
documentID = pathParts[pathParts.length - 1];
} else {
String pathSegments[] = uri.getLastPathSegment().split(":");
documentID = pathSegments[pathSegments.length - 1];
}
String mediaPath = MediaStore.Images.Media.DATA;
Cursor imageCursor = getContentResolver().query(uri, new String[]{mediaPath}, MediaStore.Images.Media._ID + "=" + documentID, null, null);
if (imageCursor.moveToFirst()) {
result = imageCursor.getString(imageCursor.getColumnIndex(mediaPath));
}
return result;
}
This work for me. 这对我有用。
I created a Util class RealPathUtil
(there are some little differences with the class in the @Artem answer): 我创建了一个Util类
RealPathUtil
(与@Artem答案中的类有一点差别):
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.CursorLoader;
import android.database.Cursor;
import android.net.Uri;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
public class RealPathUtil {
@SuppressLint("NewApi")
public static String getRealPathFromURI_API19(Context context, Uri uri) {
String filePath = "";
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 = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
column, sel, new String[]{ id }, null);
assert cursor != null;
int columnIndex = cursor.getColumnIndex(column[0]);
if (cursor.moveToFirst()) {
filePath = cursor.getString(columnIndex);
}
cursor.close();
return filePath;
}
@SuppressLint("NewApi")
public static String getRealPathFromURI_API11to18(Context context, Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
String result = null;
CursorLoader cursorLoader = new CursorLoader(
context,
contentUri, proj, null, null, null);
Cursor cursor = cursorLoader.loadInBackground();
if(cursor != null){
int column_index =
cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
result = cursor.getString(column_index);
}
return result;
}
public static String getRealPathFromURI_BelowAPI11(Context context, Uri contentUri){
Cursor cursor = null;
try {
String[] proj = { MediaStore.Images.Media.DATA };
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
assert cursor != null;
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
}
Then, in my app code, I used: 然后,在我的应用程序代码中,我使用了:
private String getPathFromUri(Uri uri) {
if (Build.VERSION.SDK_INT < 11)
return RealPathUtil.getRealPathFromURI_BelowAPI11(getContext(), uri);
else if (Build.VERSION.SDK_INT < 19)
return RealPathUtil.getRealPathFromURI_API11to18(getContext(), uri);
else
return RealPathUtil.getRealPathFromURI_API19(getContext(), uri);
}
确保您在Android Manifest中获得许可!
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.