shivanshPurple
shivanshPurple

Reputation: 302

Image unsupported when using intent to view image in gallery

I want to open an image stored in local storage in the gallery from my app. But it always shows that the image is unsupported.

Here is the code:

File fileStr = new File (Environment.getExternalStorageDirectory() + "/Storifier/test.jpg");
Uri uri = FileProvider.getUriForFile(this, this.getApplicationContext().getPackageName() + ".provider", fileStr);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "image/jpg");
startActivity(intent);

The Uri that I get for the file is

content://com.purple.myapplication.provider/external_files/Storifier/test.jpg

The image is stored in Storifier folder in the storage

I am a newbie!

Upvotes: 0

Views: 939

Answers (1)

Nour Eldien Mohamed
Nour Eldien Mohamed

Reputation: 961

try this code

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, RESULT_LOAD_IMG);

add this in your activity result

@Override
    protected void onActivityResult(int reqCode, int resultCode, Intent data) {
        super.onActivityResult(reqCode, resultCode, data);


        if (resultCode == RESULT_OK) {
            try {
                final Uri imageUri = data.getData();
                final InputStream imageStream = getContentResolver().openInputStream(imageUri);
                final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
                image_view.setImageBitmap(selectedImage);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                Toast.makeText(PostImage.this, "Something went wrong", Toast.LENGTH_LONG).show();
            }

        }else {
            Toast.makeText(PostImage.this, "You haven't picked Image",Toast.LENGTH_LONG).show();
        }
    }

and add this in manifest.xml

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

Upvotes: 2

Related Questions