Я не могу сохранить изображение в личных файлах в Android KitKat - PullRequest
0 голосов
/ 17 октября 2018

Я хочу сохранить изображение, снятое с камеры, в личных файлах и на внутреннем хранилище моего приложения, и все работает нормально, но в 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>
...