Result code 0 for Camera Intent

2019-02-26 03:08发布

问题:

Code:-

  Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);                                   
        try{
               imageUri = Uri.fromFile(File.createTempFile("image", ".jpg"));
           }catch (Exception ex){
               ex.printStackTrace();
          }

   intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageUri);
    startActivityForResult(intent, REQUEST_CAMERA);

I am using all permissions related to this in android manifest file... that above intent is working fine in mot g3 turbo and many more devices but in the case of only nexus 5 the resultCode is coming 0.. why?

回答1:

With the guidance of CommonsWare Sir, I resolved my problem by doing the following changes...

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
destination = Environment.getExternalStorageDirectory().getPath() + "/image.jpg";
outputUri= Uri.fromFile(new File(destination));
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP) {
  intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
} else {
  List<ResolveInfo> resInfoList = getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
  for (ResolveInfo resolveInfo : resInfoList) {
      String packageName = resolveInfo.activityInfo.packageName;
      grantUriPermission(packageName, outputUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
  }
}         

startActivityForResult(intent, REQUEST_CAMERA);