I am trying to develop using camera in my android application.
The problem is that the camera always returns a result code of 0, irrespective of if I press done or c
The issue (in android >= 5.0) might be with singleInstance
mode.
if you have your activity launchMode
set to singleInstance
, then in android < 5.0 you will receive cancelled result immediately. In android >=5.0 you will have resultCode == Activity.RESULT_CANCELED
.
Try using launchMode = singleTask
. It is much like singleInstance
, but allows other activities to be launched on the task.
More info here: https://developer.android.com/guide/topics/manifest/activity-element.html#lmode
I was experiencing this same issue. Camera works on 5.0+ with lauchmode=singleInstance returning a correct "RESULT_OK" but on Android 4.0 I was getting back a 0 until:
Android 4.0 switched to "launchMode=singleTask" in the AndroidManifest.xml for the Activity calling the camera resulting in "RESULT_OK" == 1
This is useful for apps with backwards compatibility.
I resolved my problem by doing the following changes...
private void takePictureFromCamera() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(activity.getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
photoFile = createImageFile();
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(activity,
"com.example.provider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
//COMPATIBILITY
if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.LOLLIPOP) {
takePictureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
} else {
List<ResolveInfo> resInfoList = activity.getPackageManager().queryIntentActivities(takePictureIntent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
activity.grantUriPermission(packageName, photoURI, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
}
//COMPATIBILITY
startActivityForResult(takePictureIntent, CAMERA);
}
}
}
I found the solution in:
link
Also, sometimes the issue causes due to not personally adding a required subfolder. The application crashes silently resulting result code as 0 every time.
As per the comments section, the reason that the resultCode
was returning 0 (meaning the result was cancelled) is because when taking a picture to save to the SD card, you need to add the WRITE_EXTERNAL_STORAGE
permission to your manifest.