0

I have an app, there is a viewPager, where I'm adding my photos from gallery.

In older versions that was working cool, but starting from android 6.0 and higher I need to ask permissions at runtime. I've done that. But the problem is that when I'm allowing the permissions, image is not loading in viewPager and after second time the image loads good.

viewPager adapter (I'm loading my images from gallery)

public class ImageAdapter extends PagerAdapter {

private ImageView imageView;
private Context context;
private LayoutInflater inflater;
public ArrayList<String> listOfAllImages = new ArrayList<>();
private static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 1;

public ImageAdapter(Context context) {
    this.context = context;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        requestRead();
        checkPermission();
    } else {
        getAllShownImagesPath();
    }
}

private void requestRead() {
    if (ContextCompat.checkSelfPermission(context,
            Manifest.permission.READ_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {

        ActivityCompat.requestPermissions((Activity) context,
                new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
    } else {
        getAllShownImagesPath();
    }
}

private void checkPermission(){
    if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
        getAllShownImagesPath();
    }
}

@Override
public int getCount() {
    return listOfAllImages.size();
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == object;
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.photopager, null);
    imageView = (ImageView) view.findViewById(R.id.photoView2);

    Glide.with(context).load(listOfAllImages.get(position))
            .thumbnail(1f)
            .crossFade()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .into(imageView);

    ViewPager viewPager = (ViewPager) container;
    viewPager.addView(view, 0);
    return view;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    ViewPager vp = (ViewPager) container;
    View view = (View) object;
    vp.removeView(view);
}

private void getAllShownImagesPath() {
    Uri uri;
    Cursor cursor;
    int column_index_data;
    String absolutePathOfImage;
    uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

    String[] projection = {MediaStore.MediaColumns.DATA,
            MediaStore.Images.Media.BUCKET_DISPLAY_NAME};

    cursor = context.getContentResolver().query(uri, projection, null,
            null, null);

    column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
    while (cursor.moveToNext()) {
        absolutePathOfImage = cursor.getString(column_index_data);
        listOfAllImages.add(absolutePathOfImage);
    }

    cursor.close();
}
}

How to load images after clicking allow in permission request?

This is adapter because I can't override onRequestPermissionsResult.

Mikhail Kholodkov
  • 23,642
  • 17
  • 61
  • 78
Hayk Mkrtchyan
  • 2,835
  • 3
  • 19
  • 61
  • have you checked my answer on this [SO](https://stackoverflow.com/questions/50807882/android-request-run-time-permission-not-working/50807900#50807900)? – Sagar Jun 13 '18 at 11:30

1 Answers1

1

Firstly, let's try to see the source of the problem you are having:

it is not working the first time because you are executing something asynchronously and expecting to get the results synchronously:

requestRead();
checkPermission();

so, when you call checkPermission(); the permission has not been granted yet.

The possible solution is:

Handle the permission request in your activity or fragment: link1 and link2, and this is the official documentation which you should be familiar as you has implemented this feature before. Then, when the permission is granted

onRequestPermissionsResult(int requestCode,String permissions[], int[] grantResults)

you can let the adapter know (adapter.readPermissionGranted() just as an example) so that it can fetch the images (getAllShownImagesPath();).

Leandro Ocampo
  • 1,894
  • 18
  • 38
  • So I don't need to do anything in adapter? Only in activity? – Hayk Mkrtchyan Jun 13 '18 at 11:42
  • if that pagerAdapter is inside a fragment I recommend you to check for permission inside the Fragment, otherwise do it inside the Activity. Then, as I wrote in my answer, let the adapter know that it has permission for reading. – Leandro Ocampo Jun 13 '18 at 11:46
  • Actually you can move all the logic of fetching the path of images in the Activity or fragment and pass the list of String as a parameter to the PagerAdapter., but that is up to you and has nothing to do with permissions :) – Leandro Ocampo Jun 13 '18 at 11:50
  • I have fragment. I done the permission check inside the fragment and I want to let the adapter know I need to call adapter.getAllShownImagesPath(); ? – Hayk Mkrtchyan Jun 13 '18 at 11:51
  • you can do it after you are sure you have permission through "onRequestPermissionsResult" or when you check for permission . – Leandro Ocampo Jun 13 '18 at 11:54
  • I tried to call adapter.getAllShownImagesPath() in onRequestPermissionsResult after checking permission granted or not but that not works) – Hayk Mkrtchyan Jun 13 '18 at 12:04
  • can you paste the code where you create the adapter and where you are call getAllShownImagesPath()? – Leandro Ocampo Jun 13 '18 at 12:06
  • Ok, but let's continue this discussion in chat. – Hayk Mkrtchyan Jun 13 '18 at 12:07
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/173068/discussion-between-name-surname-and-leandro-ocampo). – Hayk Mkrtchyan Jun 13 '18 at 12:07