gpt4 book ai didi

java - 如何从 URI android Lollipop 获取真实路径

转载 作者:行者123 更新时间:2023-12-01 18:08:43 24 4
gpt4 key购买 nike

我已经尝试过了,但没有成功。

 @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。我正在运行 Lollipop 。请有人详细解释如何做到这一点。我能够毫无问题地获取 URI,但我唯一的问题是无法将其转换为存储在数据库中的真实路径。

谢谢

最佳答案

此问题主要发生在 Nexus 等 Google 原生设备上,因为在这些设备中唯一的图像库是 Google Photos。在谷歌照片中,您不仅可以选择设备上存在的图像,还可以选择存储在云存储中的照片。尝试下面提到的您的更新代码,并让我知道它是否有效。您的 Intent 应具有 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();
}

关于java - 如何从 URI android Lollipop 获取真实路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34503687/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com