gpt4 book ai didi

android - 从图库中选择图像后从 android URI 获取真实路径

转载 作者:行者123 更新时间:2023-11-29 20:00:33 27 4
gpt4 key购买 nike

我(一个初学者 android)试图获取用户从图库中选择的图像的真实路径(例如:image_name.jpg 等)。

这是代码 fragment :

 if (requestCode == SELECT_FILE) {
Uri selectedImage = data.getData();
Bitmap bitmapImage;
try {
bitmapImage =decodeBitmap(selectedImage );
photoImage = (ImageView) findViewById(R.id.photoImage);
photoImage.setImageBitmap(bitmapImage);
photoName = (TextView) findViewById(R.id.designPhoto);
File thePath = new File(getRealPathFromURI(selectedImage));
photoName.setText(getRealPathFromURI(selectedImage));
} catch (Exception e) {
e.printStackTrace();
}
}


private String getRealPathFromURI(Uri contentURI) {

String thePath = "no-path-found";
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(contentURI, filePathColumn, null, null, null);
if(cursor.moveToFirst()){
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
thePath = cursor.getString(columnIndex);
}
cursor.close();
return thePath;
}

但是我在 photoName textView 中没有得到任何东西也尝试了很多代码和指南但没有成功。

但是当我用

尝试时
photoName.setText(selectedImage.getPath()); 

我得到了

/document/image:n (where n=any numbers) ex:/document/image:147 

但我想要一个正确的文件名或路径,例如:image_name.jpg/path/image_name.png

从过去 3 小时开始尝试,如果有人能帮助我,那就太好了。谦虚 提前致谢。

最佳答案

我用它来工作

MediaStore.Images.Media.DISPLAY_NAME

更新和工作代码可能会帮助其他人:

private String getRealPathFromURI(Uri contentURI) {

String thePath = "no-path-found";
String[] filePathColumn = {MediaStore.Images.Media.DISPLAY_NAME};
Cursor cursor = getContentResolver().query(contentURI, filePathColumn, null, null, null);
if(cursor.moveToFirst()){
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
thePath = cursor.getString(columnIndex);
}
cursor.close();
return thePath;
}

关于android - 从图库中选择图像后从 android URI 获取真实路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36336498/

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