I am trying to capture photo and need its uri after capturing it. But for some device
Uri selectedImage = data.getData();
is returning null. What should i make code change in below code to get this output?
builder.setPositiveButton(R.string.GALLARY, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getApplicationContext(), "GALLARY clicked", Toast.LENGTH_LONG).show();
Intent pickPhoto = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
output=new File(new File(getApplicationContext().getFilesDir(), PHOTOS), FILENAME);
someActivityResultLauncher.launch(pickPhoto);
}
});
//------when camera is selected to capture photo
builder.setNegativeButton(R.string.CAMERA, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int j) {
///--start this code useful for setting file path available----------
output=new File(new File(getApplicationContext().getFilesDir(), PHOTOS), FILENAME);
if (output.exists()) {
output.delete();
}
else {
output.getParentFile().mkdirs();
}
Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri outputUri= FileProvider.getUriForFile(getApplicationContext(), AUTHORITY, output);
i.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP) {
i.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
else if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN) {
ClipData clip=
ClipData.newUri(getApplicationContext().getContentResolver(), "A photo", outputUri);
i.setClipData(clip);
i.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
else {
List<ResolveInfo> resInfoList=
getApplicationContext().getPackageManager()
.queryIntentActivities(i, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
getApplicationContext().grantUriPermission(packageName, outputUri,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
}
try {
Toast.makeText(getApplicationContext(), "CAMERA clicked", Toast.LENGTH_LONG).show();
someActivityResultLauncher.launch(i);
}
catch (ActivityNotFoundException e) {
Toast.makeText(getApplicationContext(), "nocamera", Toast.LENGTH_LONG).show();
}
}
});
Overide method(onActivityResult)
ActivityResultLauncher<Intent> someActivityResultLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK) {
Intent i=new Intent(Intent.ACTION_VIEW);
Uri outputUri=FileProvider.getUriForFile(getApplicationContext(), AUTHORITY, output);
i.setDataAndType(outputUri, "image/jpeg");
i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Intent data = result.getData();
if(data==null) {
uploadImageFile(outputUri);
} else {
Uri selectedImage = data.getData();//------this is null, how to get URI value here?
// uploadImageFile(selectedImage);
}
}
}
});
I saw few answers like Camera activity returning null android , Android data.getData() returns null from CameraActivity for some phones in Stackoverflow, but i could not understood it. So if you post a answer for issue it will be helpful