[英]How to get path by MediaStore.Images.Media
I use below code to get the bitmap of all sd card photo. 我使用下面的代码来获取所有SD卡照片的位图。
String[] projection = {MediaStore.Images.Media._ID,MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, MediaStore.Images.Media._ID);
int count = cursor.getCount();
int image_column_index = cursor.getColumnIndex(MediaStore.Images.Media._ID);
path = new String[count];
bm = new Bitmap[count];
for (int i = 0; i < count; i++) {
cursor.moveToPosition(i);
int id = cursor.getInt(image_column_index);
path[i] //How to get path
bt[i] = MediaStore.Images.Thumbnails.getThumbnail(getApplicationContext().getContentResolver(), id, MediaStore.Images.Thumbnails.MICRO_KIND, null);
}
I already get the thumbnail of all photo. 我已经获得了所有照片的缩略图。 But I want to get the absolute path. 但我想要获得绝对的道路。 Where should I modify? 我应该在哪里修改?
Images.ImageColumns.DATA
is the path Images.ImageColumns.DATA
是路径
How to use it: 如何使用它:
int image_column_index = cursor.getColumnIndex(MediaStore.Images.Media._ID);
int image_path_index = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
//added ...
path[i] = cursor.getString(image_path_index);
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == yourRequestCode) {
currImageURI = data.getData();
}
}
}
// Convert the image URI to the direct file system path
public String getRealPathFromURI(Uri contentUri) {
String [] proj={MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery( contentUri,
proj, // Which columns to return
null, // WHERE clause; which rows to return (all rows)
null, // WHERE clause selection arguments (none)
null); // Order-by clause (ascending by name)
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
In Latest OS managed query
is deprecated So use 在最新的OS managed query
已弃用所以使用
yourActivity.getContentResolver().query
instead of it, Both uses the same arguments yourActivity.getContentResolver().query
而不是它,两者使用相同的参数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.