Android Q: Load image from file

336 views Asked by At

I have a problem with loading image from internal storage i have tried this code:

final String imagePath = "/storage/emulated/0/dummy.jpg";
ImageView img = findViewById(R.id.test_image);

  if (isStoragePermissionGranted()){
      File file = new File(imagePath);
      if (file.exists()) {
          Log.d("GalleryImages", "file is exist");
          Uri uri = Uri.fromFile(file);
          img.setImageURI(uri);
      }else {
          Log.d("GalleryImages", "file is not exist");
      }
   }
private   boolean isStoragePermissionGranted() {
        if (Build.VERSION.SDK_INT >= 23) {
            if (checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE)
                    == PackageManager.PERMISSION_GRANTED) {
                Log.d("GalleryImages","Permission is granted");
                return true;
            } else {

                Log.d("GalleryImages","Permission is revoked");
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
                return false;
            }
        }
        else {
            Log.d("GalleryImages","Permission is granted");
            return true;
        }
    }

and xml:

<ImageView
        android:id="@+id/test_image"
        android:scaleType="fitXY"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#63C57878" />

and result is:

D/GalleryImages: Permission is granted
D/GalleryImages: file is exist

but nothing on ImageView,

I have also tried to load image using Picaso:

Picasso.get().load(file).error(R.mipmap.ic_launcher).into(img);

but also nothing work.

Update

  • I have tried these solutions but not working for me.
  • I have tried to run code on android pie works correctly.
  • I have tried to run code on another android Q device not working.
1

There are 1 answers

0
Mohamed Slama On

After too long search i have found solution here by adding to application on AndroidManifest.xml :

android:requestLegacyExternalStorage="true"

Hope it will help other people.