由於我在不同的設備上有錯誤,我決定改變整個打開的相機並保存圖片代碼。我在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個字節的文件。
我能做些什麼來解決它?