Getting Uri from MediaStore.Images

Viewed 596

Basically what I want to do is, iterate over all the image files in my phone and to get and do OCR on them using MLKit. I am using MediaStore for iterating over the images. Here is my code:

public void onClick(View v){
            String[] projection = new String[] {
                    MediaStore.Images.Media._ID,
                    MediaStore.Images.Media.DISPLAY_NAME,
                    MediaStore.Images.Media.DATA
            };

            Cursor cursor = getApplicationContext().getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, MediaStore.Images.Media.DISPLAY_NAME+" ASC");
            //cursor.moveToNext();
            while(cursor.moveToNext()){
                Uri imageUri = Uri.parse(cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)));
                //Uri imageUri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,String.valueOf(cursor.getInt(cursor.getColumnIndex(MediaStore.Images.Media._ID))));
                final TextView yotext = findViewById(R.id.sample);
                yotext.setText(imageUri.toString());
                ImageView ima = (ImageView) findViewById(R.id.sampleImage);
                ima.setImageURI(Uri.parse(imageUri));

                //InputImage image = InputImage.fromFilePath(getApplicationContext(),imageUri);
                try {
                    InputImage image = InputImage.fromFilePath(v.getContext(), imageUri);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
}
}

I have tried Uri.parse(cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA))) and Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,String.valueOf(cursor.getInt(cursor.getColumnIndex(MediaStore.Images.Media._ID)))) to get imageUri and both methods give me different Uris. I wasn't able to display the image provided by both of these methods and ima.setImageURI(Uri.parse(imageUri)) gives the following error

2020-07-05 05:14:38.020 27990-27990/com.example.deletex E/Drawable: Unable to decode stream: android.graphics.ImageDecoder$DecodeException: Failed to create image decoder with message 'unimplemented'Input contained an error.
2020-07-05 05:14:38.021 27990-27990/com.example.deletex W/ImageView: resolveUri failed on bad bitmap uri: /storage/emulated/0/Pictures/Boost/File:ISS-32_American_EVA_b3_Aki_Hoshide.jpg

Uri returned by Uri.parse(cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA))) was /storage/emulated/0/Pictures/Boost/File:ISS-32_American_EVA_b3_Aki_Hoshide.jpg

and Uri returned by Uri imageUri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,String.valueOf(cursor.getInt(cursor.getColumnIndex(MediaStore.Images.Media._ID)))); was content://media/external/images/media/200560

0 Answers
Related