如何在Android中获取通过多部分发送到服务器的图像的完整路径
在我的应用程序中,用户可以从相机拍摄照片,或从多媒体资料中选择并裁剪照片以发送到服务器。所以没有问题。, 问题是当用户从图库中选择图片时 从相机裁剪的URI路径:/mnt/sdcard/avatar_14349583; 40804.jpg如何在Android中获取通过多部分发送到服务器的图像的完整路径,android,image,filepath,Android,Image,Filepath,在我的应用程序中,用户可以从相机拍摄照片,或从多媒体资料中选择并裁剪照片以发送到服务器。所以没有问题。, 问题是当用户从图库中选择图片时 从相机裁剪的URI路径:/mnt/sdcard/avatar_14349583; 40804.jpg 从存储裁剪的URI路径:/external/images/media/19 错误: IOException:/external/images/media/19:打开失败:enoint(无此项) 文件或目录) 您应该从URI获取路径。使用以下功能: privat
从存储裁剪的URI路径:/external/images/media/19 错误:
IOException:/external/images/media/19:打开失败:enoint(无此项) 文件或目录)
您应该从URI获取路径。使用以下功能:
private String getRealPathFromURI(Uri contentURI) {
//Log.e("in","conversion"+contentURI.getPath());
String path;
Cursor cursor = getContentResolver()
.query(contentURI, null, null, null, null);
if (cursor == null)
path=contentURI.getPath();
else {
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
path=cursor.getString(idx);
}
if(cursor!=null)
cursor.close();
return path;
}
截取相机的URI路径:/mnt/sdcard/avatar_1434958340804.jpg 从存储裁剪的URI路径:/external/images/media/19
@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);
int columnIndex = 0;
if (cursor != null) {
columnIndex = cursor.getColumnIndex(column[0]);
}
if (cursor != null && cursor.moveToFirst()) {
filePath = cursor.getString(columnIndex);
}
if (cursor != null) {
cursor.close();
}
Log.e("GGGGG FilePath",filePath);
return filePath;
}
在API 19之后,内容URI发生了变化
content://com.android.providers.media.documents/document/image%3A151323
及
/storage/3437-6630/Pics/IMG_5737.JPG
在API19之前调用gallery Intent将导致gallery。但是在API19之后将导致调用图像。您可以从这里进入Gallery、外部存储或Google drive。因此有机会获得运行时异常
MainActivity.java
在RealPathUtil.java中
getRealPathFromURI()
getRealPathFromURI\u API19
@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);
int columnIndex = 0;
if (cursor != null) {
columnIndex = cursor.getColumnIndex(column[0]);
}
if (cursor != null && cursor.moveToFirst()) {
filePath = cursor.getString(columnIndex);
}
if (cursor != null) {
cursor.close();
}
Log.e("GGGGG FilePath",filePath);
return filePath;
}
/external目录是错误的。为什么您认为应该是URI?
MediaStore.Images.Media.DATA
被删除了。现在这已经不起作用了
@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);
int columnIndex = 0;
if (cursor != null) {
columnIndex = cursor.getColumnIndex(column[0]);
}
if (cursor != null && cursor.moveToFirst()) {
filePath = cursor.getString(columnIndex);
}
if (cursor != null) {
cursor.close();
}
Log.e("GGGGG FilePath",filePath);
return filePath;
}