我用这个方法来从URI得到的文件路径:错误从Download文件夹路径获取图像的Android
public String getPath(Uri uri) {
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
String document_id = cursor.getString(0);
document_id = document_id.substring(document_id.lastIndexOf(":") + 1);
cursor.close();
cursor = getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
null, MediaStore.Images.Media._ID + " = ? ", new String[]{document_id}, null);
cursor.moveToFirst();
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
cursor.close();
return path;
}
如果我尝试从一个图像文件获取文件的路径,从画廊的作品完美。但是,当我尝试从下载文件夹中的图像文件获取路径文件时,不起作用。有什么建议么。非常感谢你提前。
Hi CommonsWare。非常感谢你真的很好的答案。选择图像后,Uri被正确传回。但是当我尝试获取donwload文件夹中该图像的实际路径时,它会崩溃。这个问题似乎是特定于这个特定的文件夹。我不知道如何管理ContentResolver或openInputStream()。那么,我怎么能得到这个文件夹中的实际位置? – agustincola
@agustincola:“我怎么能得到这个文件夹中的实际位置?” - 你没有。您可以使用'Uri'访问与内容相关的流,使用'ContentResolver'。这应该包含在你的Android应用程序开发书或课程中。它也包含在[文档](https://developer.android.com/training/secure-file-sharing/request-file.html)中。 – CommonsWare
好的。感谢您的时间。 – agustincola