Search code examples
android-fragmentsandroid-permissionspicassoandroid-architecture-navigationuser-permissions

Read User Selected Image in Another Fragment, and Across App Restarts


My app uses the navigation component and thus has a single activity with multiple fragments.

  1. In an AccountsFragment, I allow the user create a new account using a fab, which then displays a dialogue. Inside the dialogue, the user can click the avatar icon which is supposed to open the system picker. Below is the code of the onClick (for avatar image pick):

    if (ContextCompat.checkSelfPermission( requireContext(), Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { //FROM HERE YOU CAN READ THEIR DATA.

             Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
             intent.setFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
             intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
             intent.addCategory(Intent.CATEGORY_OPENABLE);
             intent.setType("image/*");
    
             startActivityForResult.launch(intent);}
    

The above code then opens a picker, and I select an image (I have also handled asking for permission if it's not granted)

In on activity results, my code is as follows:

 if (result.getResultCode() == Activity.RESULT_OK) {
        Intent data = result.getData();
        Uri contentUri = data.getData();
        System.out.println("the returned uri is " + contentUri);

        final int takeFlags = intent.getFlags()
                & (Intent.FLAG_GRANT_READ_URI_PERMISSION
                | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);



        requireActivity().getContentResolver().takePersistableUriPermission(contentUri, takeFlags);

        subscriberImageUri = uri.toString(); // this is a string i store in my sqlite db. 

        Picasso.get().load(contentUri).into(subscribersImage);


    }

The problems:

  1. The image only appears in the dialogue, and not in the account list recyclerview the fragment uses as layout. This is how I set the image after user clicks create account button:

    newSubscriber.setSubscriberImageURi(subscriberImageUri); subscribersArrayList.add(newSubscriber); subscribersAdapter.notifyDataSetChanged();

Piccasso simply does not load an image (the code is in the recycler adapter) and does not throw an error.

  1. After persisting the URI and saving a string of it to sqlite, how can I get the string to still point to the same image that was selected? This is my code in the account details fragment (which is supposed to display the profile image that was selected in account list fragment:

    List uriPermissions = requireActivity().getContentResolver().getPersistedUriPermissions(); for(UriPermission uriPermission: uriPermissions){

         Uri data = uriPermission.getUri();
         profile.setImageURI(data);
         Picasso.get().load(data).into(profile); // this is just to test for the first user created.
     }
    

My manifest permissions:

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

Kindly help, I have been stuck on this problem for days. I am available to provide further clarifications.


Solution

  • The above code works perfectly for persisting uri permission. The problem was that I used a tint on the imageview, and the selected image was being blocked by the tint.