我无法将图像保存在Android KitKat中的私人文件中

时间:2018-10-17 05:13:40

标签: android android-4.4-kitkat android-fileprovider

我想将从相机拍摄的图像保存在我的应用程序的私有文件和内部存储中,并且一切正常,但是在android 4.4中,当我要将其保存在私有文件中时,该图像未保存。

这是我的代码:

public static void dispatchTakePictureIntent(Fragment fragment) {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    boolean isKitKat = isKitKat();
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(fragment.getActivity().getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile(fragment.getActivity());
        } catch (IOException ex) {
            // Error occurred while creating the File
            ex.getMessage();
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            Uri photoURI=null;

            photoURI = (isKitKat) ?
                    Uri.fromFile(photoFile) : FileProvider.getUriForFile(fragment.getActivity(),
                    "com.example.android.fileprovider", photoFile);

            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            fragment.startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }
}

public static File createImageFile(Activity activity) throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    isInternalStorage = MyApplication.getPreferences().getSaveLocation();
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = (isInternalStorage==StorageLocation.INTERNAL_STORAGE.getLocation()) ? Environment.getExternalStorageDirectory() : activity.getFilesDir();
    storageDir = new File(storageDir,File.separator+FOLDER_MAIN+File.separator+FOLDER_PICTURES+File.separator+FOLDER_PHOTOS);
    Log.i(TAG,"final path: "+ storageDir.getAbsolutePath());
    File image = new File(storageDir,imageFileName.concat(".jpg"));
    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = image.getAbsolutePath();
    return image;
}

    <?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="my_images_internal" path="test/pictures/photos" />
    <files-path name="my_images_private" path="test/pictures/photos"/>
</paths>

0 个答案:

没有答案