How to get path by MediaStore.Images.Media

后端 未结 2 1100
眼角桃花
眼角桃花 2020-12-20 02:38

I use below code to get the bitmap of all sd card photo.

String[] projection = {MediaStore.Images.Media._ID,MediaStore.Images.Media.DATA};  
Cursor cursor =          


        
相关标签:
2条回答
  • 2020-12-20 02:52

    Images.ImageColumns.DATA is the path

    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);
    
    0 讨论(0)
  • 2020-12-20 03:08
    @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

    yourActivity.getContentResolver().query instead of it, Both uses the same arguments

    0 讨论(0)
提交回复
热议问题