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

时间:2012-01-05 02:57:34

标签: android

我使用下面的代码来获取所有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);
}

我已经获得了所有照片的缩略图。 但我想要获得绝对的道路。 我应该在哪里修改?

2 个答案:

答案 0 :(得分:7)

Images.ImageColumns.DATA是路径

如何使用它:

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);

答案 1 :(得分:1)

@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);
}

在最新操作系统中managed query已弃用,请使用

yourActivity.getContentResolver().query而不是它,两者都使用相同的参数