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.