2016-12-26 69 views
0

由於我在不同的設備上有錯誤,我決定改變整個打開的相機並保存圖片代碼。我在Android tutorial中使用了完全相同的代碼。Android - 保存照片到路徑 - RESULT_CANCELLED

我的代碼:

private static File createImageFile(Activity activity) throws IOException { 
    // Create an image file name 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
    String imageFileName = "JPEG_" + timeStamp + "_"; 
    File storageDir = activity.getExternalFilesDir(Environment.DIRECTORY_PICTURES); 
    File image = File.createTempFile(
      imageFileName, /* prefix */ 
      ".jpg",   /* suffix */ 
      storageDir  /* directory */ 
    ); 

    // Save a file: path for use with ACTION_VIEW intents 
    photoPath = image.getAbsolutePath(); 
    return image; 
} 

private static void dispatchTakePictureIntent(Activity activity) { 
    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; 
     try { 
      photoFile = MainActivity.createImageFile(activity); 
     } catch (IOException ex) { 
      // Error occurred while creating the File 
     } 
     // Continue only if the File was successfully created 
     if (photoFile != null) { 
      imageUri = FileProvider.getUriForFile(activity, 
        "com.APPPACKAGE.fileprovider", 
        photoFile); 
      takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); 
      activity.startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO); 
     } 
    } 
} 

在manifest文件:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" 
android:maxSdkVersion="18" /> 

<provider 
    android:name="android.support.v4.content.FileProvider" 
    android:authorities="com.APPPACKAGE.fileprovider" 
    android:exported="false" 
    android:grantUriPermissions="true"> 
    <meta-data 
     android:name="android.support.FILE_PROVIDER_PATHS" 
     android:resource="@xml/file_paths"/> 
</provider> 

使用該上的Android 6棉花糖的偉大工程,在Android 4.4奇巧事實並非如此。在Kitkat我的onActivityResult我從相機result = 0收到,這是RESULT_CANCELLED

我檢查過相機是否能夠將照片保存在file_paths.xml中指定的位置,但沒有。此文件夾充滿了0個字節的文件。

我能做些什麼來解決它?

回答

0

由於@CommonsWare我加入這個代碼,它的工作原理:

if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP) { 
       takePictureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); 
      } 
      else if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN) { 
       ClipData clip= 
         ClipData.newUri(activity.getContentResolver(), "A photo", imageUri); 

       takePictureIntent.setClipData(clip); 
       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, imageUri, 
          Intent.FLAG_GRANT_WRITE_URI_PERMISSION); 
       } 
      } 
1

並非所有的相機應用都支持content作爲EXTRA_OUTPUTUri的方案。例如,Google自己的相機應用直到2016年夏天才支持它。而且,由於我們通過了額外的Uri,所以我們無法將自己限制爲支持content的相機應用。

你的主要選項有:

  1. 減少你targetSdkVersion低於24,並與Uri.fromFile()堅持,而不是使用FileProvider

  2. 使用StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().build());禁用FileUriExposedException,然後用Uri.fromFile()堅持,而不是使用FileProvider

  3. 完全廢除您使用ACTION_IMAGE_CAPTURE,switchi直接或通過一些輔助庫NG到相機的API(例如,mine

戰術上,你可能會得到更好的結果,如果你use setClipData() to force granting of permissions on your Uri