Kartik Thakur
Kartik Thakur

Reputation: 11

Saving image to gallery

I am building an application in which I want to capture an image using intent (MediaStore.ACTION_IMAGE_CAPTURE) and want to save it to gallery. Every thing is working fine but image is not being saved to gallery. I have followed google documentation (https://developer.android.com/training/camera/photobasics) to perform my task. But image is not being saved to gallery.

My manifest file :

.
.
.
 <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.kartik.translater"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths"></meta-data>
        </provider>
.
.

my file_paths.xml file:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<!--    <external-files-path name="my_images" path="Pictures" />-->
<!--    <external-path name="external_files" path="." />-->
    <external-path
        name="external"
        path="."/>
    <external-files-path
        name="external_files"
        path="."/>
    <cache-path
        name="cache"
        path="."/>
    <external-cache-path
        name="external_cache"
        path="."/>
    <files-path
        name="files"
        path="."/>
</paths>

This set of instruction will trigger image capturing process:

 //Capture Input Image
        camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dispatchTakePictureIntent();
            }
        });

dispatchTakePictureIntent() definition :

private void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // Ensure that there's a camera activity to handle the intent
        if (takePictureIntent.resolveActivity(getPackageManager()) == null) {
            // Create the File where the photo should go
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
                // Error occurred while creating the File
                Toast.makeText(MainActivity.this, "Failure", Toast.LENGTH_SHORT).show();
            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                Uri photoURI = FileProvider.getUriForFile(this,
                        "com.kartik.translater",
                        photoFile);
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, CAMERA_REQUEST_CODE);
            }
        }
    }

createImageFile() fn. definition:

private File createImageFile() throws IOException {
            // Create an image file name
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
            String imageFileName = "JPEG_" + timeStamp + "_";
            File storageDir =  getExternalFilesDir(Environment.DIRECTORY_PICTURES);
            File image = File.createTempFile(
                    imageFileName,  /* prefix */
                    ".jpg",         /* suffix */
                    storageDir      /* directory */
            );

            // Save a file: path for use with ACTION_VIEW intents
            currentPhotoPath = image.getAbsolutePath();
            return image;
        }

onActivityResult() defintion:

 if(requestCode==CAMERA_REQUEST_CODE && resultCode==RESULT_OK)
        {
            Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            File f = new File(currentPhotoPath);
            Uri contentUri = Uri.parse(currentPhotoPath);
            mediaScanIntent.setData(contentUri);
            this.sendBroadcast(mediaScanIntent);
            Toast.makeText(MainActivity.this, contentUri+"", Toast.LENGTH_SHORT).show();
        }

** I am using Android 11 to run the app. ***Toast message of onActivityResult() is getting executed but image is not getting saved in gallery.

Upvotes: 0

Views: 172

Answers (1)

Vitaliy
Vitaliy

Reputation: 66

In order to be able to write to external storage on android 10+, you need to have access to it. To provide this capability in your app, use the ACTION_OPEN_DOCUMENT_TREE intent action. More details can be found here: https://developer.android.com/training/data-storage/shared/documents-files?hl=ru#grant-access-directory

Upvotes: 0

Related Questions