Home > Blockchain >  Android Security Exception on imageView.setImageUri with album image Uri
Android Security Exception on imageView.setImageUri with album image Uri

Time:02-11

The image source is a Uri which is stored in Firestore. If a user saves their profile photo from either camera or album, it gets its Uri then stores as a string in the Firestore. There's no problem until this point.

I'll say the photo taken from Camera as CameraImage, and the photo chosen from the album as AlbumImage.

But when displaying the AlbumImage's Uri(as String) retrieved from Firestore, Security Exception is thrown.

Possible solutions I'm assuming are:

  • need to save the Uri in a different way.
  • need to load Uri in a different way.

Loding the image(error on this line):

iv_photo.setImageURI(Uri.parse(mUser.getPhotoString()));

Activity for result:

Intent selectIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        selectIntent.setType("image/*");
        galleryResultLauncher.launch(selectIntent);
    ActivityResultLauncher<Intent> galleryResultLauncher = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {

                @Override
                public void onActivityResult(ActivityResult result) {

                    if(result.getResultCode() == Activity.RESULT_OK){

                        Uri selectedImg = result.getData().getData();
                        iv_photo.setImageURI(selectedImg);
                        mUri = selectedImg.toString();
                    }
                }
            });

Security Exception:

Permission Denial: opening provider com.google.android.apps.photos.contentprovider.impl.MediaContentProvider from ProcessRecord{c7fba1f 23624:com.example.seanlee_thefootballgallery_2201/u0a165} (pid=23624, uid=10165) that is not exported from UID 10118

AlbumImage URIs is similar to this.

content://com.example.seanlee_thefootballgallery_2201/app_images/10-02-2022-09-17-01.jpg

CameraImage URIs is similar to this.

content://com.google.android.apps.photos.contentprovider/-1/1/content://media/external/images/media/25/ORIGINAL/NONE/image/jpeg/967503137

Manifest:

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        android:maxSdkVersion="28" />

CodePudding user response:

you can upload your photo to firebase storage first, you can read this upload file to firebase storage .after that you can add more code like this to get Url from storage

StorageReference storageReference = FirebaseStorage.getInstance().getReference().child() 
        //that child is up to you
        storageReference.putFile(imageUri).addOnCompleteListener(task ->
                storageReference.getDownloadUrl().addOnSuccessListener(uri -> {
            String url = uri.toString();
        }));

after that you can save url into firestore. if you want show it into your app you should get url from firestore first after that download it from firebase storage. for download file you can read this download file from firebase storage

hope it can solve your problem :)

CodePudding user response:

Solved.

Changing intent action from ACTION_PICK to ACTION_OPEN_DOCUMENT solved the issue.

  • Related