我试过这个但没有运气。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
nameView.setText(selectedImage.toString());
realPath = getRealPathFromURI(this, selectedImage);
Toast.makeText(MainActivity.this, realPath, Toast.LENGTH_LONG).show();
}
}
public String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = { MediaStore.Images.Media.DATA };
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);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
当我在TOAST中显示时,我变为null。 我正在跑棒棒糖。 请有人详细解释如何执行此操作。 我能够毫无问题地获取URI,但我唯一的问题是无法将其转换为存储在我的数据库中的真实路径。
谢谢
答案 0 :(得分:4)
此问题主要发生在谷歌本机设备(如Nexus)上,因为在这些设备中,唯一的图像库是谷歌照片。在谷歌照片中,您不仅可以选择设备上存在的图像,还可以选择存储在云存储中的照片。试试下面提到的你的更新代码,让我知道它是否有效。 您的意图应该采取 Intent.ACTION_GET_CONTENT
的操作@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == ATTACHMENT_PHOTO_GALLERY && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
nameView.setText(selectedImage.toString());
String realPath = getRealPathFromURI(this, selectedImage);
Toast.makeText(MainActivity.this, realPath, Toast.LENGTH_LONG).show();
}
}
public String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = null;
String path = null;
try {
String[] proj = {MediaStore.Images.Media.DATA};
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
path = cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
//The above code will fail to get file path if the file is selected from google photos.
//Then this will be the alternative approach
if (path == null && contentUri.toString().startsWith("content://com.google.android.apps.photos.content")) {
Bitmap requiredImage = null;
//Below key should be used for saving the newly downloaded image path as
//photoIdentificationKey as Key parameter and newly generated path as Value.
//An additional check should be implemented on your storage that whether any local
//path exists for corresponding photoIdentificationKey, if yes then use the existing
//local path else download new image.
String photoIdentificationKey = Uri.parse(contentUri.getLastPathSegment()).getLastPathSegment();
BitmapFactory.Options options = new BitmapFactory.Options();
InputStream inputStream;
try {
inputStream = getApplication().getContentResolver().openInputStream(contentUri);
requiredImage = BitmapFactory.decodeStream(inputStream, null, options);
//Save the newly downloaded image to your isolated storage and return the path
//on which this new image has been saved.
inputStream.close();
path = saveAndReturnPathForSavedImage(requiredImage);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
return path;
}
private String saveAndReturnPathForSavedImage(Bitmap bmp) {
String path = Environment.getExternalStorageDirectory().toString();
OutputStream out = null;
File file = new File(path, "IMAGE"+ Calendar.getInstance().getTime()+".jpg"); // the File to save to
try {
out = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return file.getPath();
}
答案 1 :(得分:0)
如果您的Intent data
数据不为空,那么您可以试试这个。
我已经使用此代码从图库中获取图像路径,并且它可以在所有设备中使用。
Uri selectedImage = data.getData();
String[] filePath = {MediaStore.Images.Media.DATA};
Cursor c = getContentResolver().query(selectedImage, filePath, null, null, null);
c.moveToFirst();
int columnIndex = c.getColumnIndex(filePath[0]);
String picturePath = c.getString(columnIndex);
c.close();
Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));
Log.e("path of image from gallery......******************.........", picturePath + "");
注意:请确保data
不应该为空,否则会NullPointerException
。