0

I am working on an app and in the most important activity the following has to occur:

  • User fills a form and selects image from his phone.
  • When he clicks add data, I want to save the selected image (or it's path, anything that enables me to retrieve it later) to local database and retrieve later in other activity.

Things I have tried:

  • Save image to db as blob and retrieve later as bitmap, but obviously I learned that this fails when an image larger in size is used, the blob is set to null.
  • Tried getting file path of the selected image, but that file path is in a format similar to this: /document/msf:35

Then I tried to get the physical path using these SO answers: link1, link2, link3 and these answers in general: link4, link5.

Please don't mark this as duplicate because the solution in other answers don't work for me

Opening intent to upload photo:

// upload photo
photoActivityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
  public void onActivityResult(ActivityResult result) {
    if (result.getResultCode() == Activity.RESULT_OK) {
      Intent intent = result.getData();
      if (intent.getData() == null) {
        Log.d("AddTrip", "null data from image");
      }
      Uri uri = intent.getData();
      // processing here..
    }
  }
});

}
private void uploadPhotoClicked() {
  Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
  intent.setType("image/*");
  photoActivityResultLauncher.launch(intent);
}

Please suggest a solution to work around this and also the best practice to implement this type of functionality, I have tried using PathUtil, but it doesn't give path for some images.

Sebastian Richner
  • 782
  • 1
  • 13
  • 21
Ali Ahmed
  • 164
  • 2
  • 12

1 Answers1

0

As @blackapps suggested to use ACTION_OPEN_DOCUMENT, I found the solution. First I recieved Uri dataString by intent.getDataString(), then I parsed it so I could call takePersistableUriPermission(). Now I can retrieve the Uri from db and call setImageUri(uri), to retrieve the image.

photoActivityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
            public void onActivityResult(ActivityResult result) {
                if (result.getResultCode() == Activity.RESULT_OK) {
                    Intent intent = result.getData();
                    if (intent.getData() == null) {
                        Log.d("AddTrip", "null data from image");
                    }
                        image = intent.getDataString();
                        Uri uri = Uri.parse(image);
                         context.getContentResolver().takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);

                        imageName.setText(image);
                }
            }
        });

    }

 private void uploadPhotoClicked() {
        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
        intent.setType("image/*");
        photoActivityResultLauncher.launch(intent);
    }
Ali Ahmed
  • 164
  • 2
  • 12