简体   繁体   English

如何通过MediaStore.Images.Media获取路径

[英]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.

相关问题 空光标查询MediaStore.Images.Media - Empty Cursor query MediaStore.Images.Media MediaStore.Images.Media无法显示所有图像,解决方案 - MediaStore.Images.Media is not capable of showing all images, solution 如何在 Android 上的 MediaStore.Images.Media 选择图像后调整图像大小 - How to resize image after MediaStore.Images.Media selected image on Android 使用MediaStore.Images.Media将位图保存在特定文件夹中 - Using MediaStore.Images.Media to save bitmap in particular folder 使用 MediaStore.Images.media 存储图像以保存在特定文件夹中 - STORING IMAGE using MediaStore.Images.media to save in a particular folder 从 MediaStore.Images.Media 获取图片方向 - Getting picture orientation from MediaStore.Images.Media 查询 MediaStore.Images.Media ,如何同时查询 EXTERNAL_CONTENT_URI 和 INTERNAL_CONTENT_URI? - Querying MediaStore.Images.Media , how do I query both on EXTERNAL_CONTENT_URI and INTERNAL_CONTENT_URI? 如何在不使用 MediaStore.Images.Media.DATA 的情况下通过使用 uri 获取图像真实路径 - How to get image real path by using uri without using MediaStore.Images.Media.DATA Android如何获取MediaStore的路径? - Android how to get path of mediastore? 如何从 MediaStore.Images.Media.getBitmap(contentResolver, uri) 获取位图? - How to get Bitmap from MediaStore.Images.Media.getBitmap(contentResolver, uri)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM