I have used following code for pick image from gallery. It is working till android 13 (Api 33) but it not working (gallery is not opening) on Android 14 (Api 34).
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
photoPickerIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
if (photoPickerIntent.resolveActivity(getPackageManager()) != null) {
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, PICK_IMAGE_MULTIPLE);
}
Please help me in this. Thanks in advance.
This is what I used in my project and it works on the latest android. mine has a firebase upload part it may be included in answer so use it as reference to help guide you.
Uri FilePathUri;
int Image_Request_Code = 7;
private ActivityResultLauncher<String> imagePickerLauncher;
in onCreate
imagePickerLauncher = registerForActivityResult(
new ActivityResultContracts.GetContent(),
result -> {
if (result != null) {
// Handle the selected image
FilePathUri = result;
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), FilePathUri);
imgView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
);
btnBrowse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
imagePickerLauncher.launch("image/*");
}
});
outside onCreate
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == Image_Request_Code && resultCode == RESULT_OK && data != null && data.getData() != null) {
FilePathUri = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), FilePathUri);
imgView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public String GetFileExtension(Uri uri) {
ContentResolver contentResolver = getContentResolver();
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
return mimeTypeMap.getExtensionFromMimeType(contentResolver.getType(uri));
}