how to get size of my image file store in DIRECTORY_Picture

35 Views Asked by At

I am implementing an app using camera to capture photo . But the camera is capturing even if i don't click , and saving it in the Picture Directory . Photos captured without clicking are saved in hte directory with 0 o size . As solution , i wanted check the size of each image using their path to get their size but the problem is when i try to get their size , even photos that have size ( photos ,i've captured by clicking) return 0o as their size but i want to delete them. How can i figure out it ?

    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);
        } 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) {


        }
    }

    
1

There are 1 best solutions below

2
Ayush Patel On

Ok, I got you. This may helps:-

   File imageFile = new File(currentPhotoPath);
long lastModified = imageFile.lastModified();
Date lastModifiedDate = new Date(lastModified);
Date currentDate = new Date();
if(lastModifiedDate.after(currentDate)){
    long length = imageFile.length();
    if(length == 0){
        Log.d("Image size: ","0, deleting the image");
        imageFile.delete();
    }
    else{
        double sizeInKb = length / 1024;
        double sizeInMb = sizeInKb / 1024;
        Log.d("sizeInKb", Double.toString(sizeInKb));
        Log.d("sizeInMb", Double.toString(sizeInMb));
    }
}