Android 如何使用内容解析器从gallery中的图像获取路径数据
我已经成功从gallery中获取图像,但无法将图像上载到服务器,因为该文件为空。是否有我遗漏的代码需要添加?我添加了imageView.getPath,但我只获取从相机图像到服务器的路径,而从gallery获取空图像 我找到了这条路,content://com.android.providers.media.documents/document/image%3A5755 但仍然无法上传到服务器Android 如何使用内容解析器从gallery中的图像获取路径数据,android,imageview,android-contentresolver,Android,Imageview,Android Contentresolver,我已经成功从gallery中获取图像,但无法将图像上载到服务器,因为该文件为空。是否有我遗漏的代码需要添加?我添加了imageView.getPath,但我只获取从相机图像到服务器的路径,而从gallery获取空图像 我找到了这条路,content://com.android.providers.media.documents/document/image%3A5755 但仍然无法上传到服务器 private void getImageFromGallery(Intent data) {
private void getImageFromGallery(Intent data) {
mSelectedImgURI = data.getData();
mimeType = getImageExt(mSelectedImgURI);
uploadDialog.dismiss();
imgCategory.setImageURI(mSelectedImgURI);
}
private void getImageFromCamera(Intent data) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
String tempFileName = System.currentTimeMillis() + ".jpg";
File destination = new File(Environment.getExternalStorageDirectory(),tempFileName);
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
mSelectedImgURI = Uri.fromFile(destination);
uploadDialog.dismiss();
imgCategory.setImageBitmap(thumbnail);
} catch (IOException e) {
Log.d(TAG, "Internal error - " + e.getLocalizedMessage());
}
}
public String getImageExt(Uri uri){
ContentResolver contentResolver = getApplicationContext().getContentResolver();
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
return mimeTypeMap.getExtensionFromMimeType(contentResolver.getType(uri));
}
这是我通常使用的一种方法
public String getRealPathFromURI(Uri uri) {
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
return cursor.getString(idx);
}
这将返回文件位置,您可以通过调用
File realFile = new File(getRealPathFromURI(mSelectedImgUri));
这个答案的可能重复应该有很多喜欢,它的工作