Search code examples
javaandroidandroid-cameraandroid-camera2android-camerax

Resgistering photos issues in android programming


I am implementing an android app using camera to take pictures then registering it in External_storage\Android\data\com.abc.projectname.bf . But after openning the camera , even if i don't capture , an image file is registered in the directory . How can i solve that , here is the code :

public void onCapturePhoto(String fileName){
    //Intent to take photos


    File storageDirectory = requireActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    try {
        Log.d("#picName",fileName);
        File imageFile = File.createTempFile(fileName,".jpg",storageDirectory);

            currentPhotoPath = imageFile.getAbsolutePath();
            Uri imageUri= FileProvider.getUriForFile(requireActivity(),"com.ticanalyse.mheath.bf",imageFile);
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
        startActivityForResult(intent,1);
            Log.d("#image_length is ",String.valueOf(imageFile.length()));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

//

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == RESULT_OK) {
            //  imageView.setImageBitmap(imageBitmap);
//            File f = new File(currentPhotoPath);
////            Uri contentUri = Uri.fromFile(f);
//            Bitmap bitmap = BitmapFactory.decodeFile(currentPhotoPath);
//            //Convert bitmap to byteArray
//            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//            bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
//            byte[] byteArray = byteArrayOutputStream .toByteArray();
//            //Then convert to base64
//            encodedImage= Base64.encodeToString(byteArray, Base64.DEFAULT);
//            Log.d("#base64",encodedImage);


            //   setPic();

        }
    }

Solution

  • First of all, have the imageFile object from this line:

    File imageFile = File.createTempFile(fileName,".jpg",storageDirectory);
    

    ...declared as a global in the Activity like this...

    public class XYZActivity extends AppCompatActivity {
        private File imageFile;
        //onCreate(), onCapturePhoto(), etc methods
    }
    

    Then in onCapturePhoto():

    imageFile = File.createTempFile(fileName,".jpg",storageDirectory);
    

    Now in the onActivityResult(), if you ensure that no image was taken/clicked, you could try deleting the temporary file like this:

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == RESULT_OK) {
             //Your stuff...
        }
        else {
            //No photo was taken or clicked...
            //Thus, deleting the temporary file...
            if (file.exists()) {
                if (file.delete()) {
                    Log.i("Process", "Deletion DONE!");
                }
            }
        }
    }
    

    If I interpreted any part of problem in a undesirable way, please let me know.