Ziad H.
Ziad H.

Reputation: 699

How to save a picture in a specific folder just for my app (correctly)?

The problem is that my app saves the picture twice; one in the camera folder and the other in the folder I specified. but when I tested the app on another device, that didn't happen!

//lunch the camera and make a file to save the image in and pass it with the camera intent
    public void lunchCamera() {
        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
                ex.printStackTrace();
            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                Uri photoURI = FileProvider.getUriForFile(this,
                        "com.ziad.sayit",
                        photoFile);

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

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

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

So, I want a solution for this, and I also want to save the picture in a folder for my app inside the Pictures Directory.. thanks

Upvotes: 0

Views: 653

Answers (2)

Manoj Kumar
Manoj Kumar

Reputation: 362

Call below method from OnActivityResult.

private void saveImage(Bitmap  image, String fileName) {

    File direct = new File(Environment.getExternalStorageDirectory() + "/DirName");
    if (!direct.exists()) {
        File directory = new File("/sdcard/DirName/");
        directory.mkdirs();
    }

    File file = new File(new File("/sdcard/DirName/"), fileName);
    if (file.exists()) {
        file.delete();
    }
    try {
        FileOutputStream out = new FileOutputStream(file);
        image.compress(Bitmap.CompressFormat.JPEG, 100, out);
        out.flush();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1007554

There is no solution for it, other than to not use ACTION_IMAGE_CAPTURE.

ACTION_IMAGE_CAPTURE delegates picture-taking to an arbitrary third-party camera app. There are dozens, if not hundreds, of these pre-installed on devices. There are hundreds more available for download from the Play Store and elsewhere. What they do in response to that Intent action is up to them. Ideally, they would only store the image in the location specified by EXTRA_OUTPUT. However, there is no requirement that they behave that way. Some camera apps will store the image twice, once in its normal location and once in the EXTRA_OUTPUT. Some will ignore EXTRA_OUTPUT entirely.

If this concerns you, do not use ACTION_IMAGE_CAPTURE. Use a library — CameraX, Fotoappart, CameraKit-Android, etc. — to take the photos within your own app.

Upvotes: 2

Related Questions