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?