2016-12-26 3 views
0

Da ich Fehler auf verschiedenen Geräten hatte, entschied ich, die ganze offene Kamera zu ändern und Bildcode zu speichern. Ich habe den exakt gleichen Code in der Android tutorial verwendet.Android - Foto in Pfad speichern - RESULT_CANCELLED

Mein Code:

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

Auf der Manifest-Datei:

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

diese Verwendung funktioniert auf einem Android 6 Eibisch, auf Android 4.4 Kitkat tut es nicht. Auf Kitkat meine onActivityResult Ich erhalte von der Kamera result = 0 die RESULT_CANCELLED ist.

Ich habe überprüft, ob die Kamera das Foto an dem in der file_paths.xml angegebenen Speicherort speichern konnte und es nicht tat. Dieser Ordner enthält 0 Bytes.

Was kann ich tun, um es zu beheben?

Antwort

0

Dank @CommonsWare hinzugefügt ich diesen Code und es funktioniert:

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

Nicht alle Kamera-Apps unterstützen content als Schema für die EXTRA_OUTPUTUri. Die eigene Kamera-App von Google hat es beispielsweise erst im Sommer 2016 unterstützt. Und da wir die Uri über ein Extra weitergeben, haben wir keine Möglichkeit, uns auf Kamera-Apps zu beschränken, die content unterstützen.

Ihre wichtigsten Optionen sind:

  1. Reduzieren Sie Ihre targetSdkVersion unter 24 und halten mit Uri.fromFile(), anstatt FileProvider

  2. Verwenden StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().build()); die FileUriExposedException zu deaktivieren, dann Stick mit Uri.fromFile(), anstatt FileProvider

  3. Schrott Ihre Verwendung von ACTION_IMAGE_CAPTURE vollständig, switchi ng zu dem Kamera-APIs direkt oder über einige Helfer Bibliothek (zum Beispiel mine)

Taktisch, können Sie bessere Ergebnisse, wenn Sie use setClipData() to force granting of permissions on your Uri bekommen.

Verwandte Themen