Camera Intent的结果代码0

时间:2016-06-15 12:30:03

标签: android android-camera

代码: -

  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);

我在android清单文件中使用与此相关的所有权限... 上面的意图在mot g3 turbo和更多设备上工作得很好,但在只有nexus 5的情况下,resultCode才会出现0 ..为什么?

1 个答案:

答案 0 :(得分:5)

在CommonsWare Sir的指导下,我通过以下更改解决了我的问题...

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);