0

In my app I need to pick images from storage and display them on screen. I'm using an Intent to do this for me. I have posted my code to do this below.

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");                                      
        intent.addCategory(Intent.CATEGORY_OPENABLE);

        try {
            startActivityForResult(
                    Intent.createChooser(intent, "Select a File to Upload"),
                    FILE_SELECT_CODE);
        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(this, "Please install a File Manager.",
                    Toast.LENGTH_SHORT).show();
        }

The issue is that when the intent opens, I see a bunch of empty files of size 0 B. These files are not visible in the File Manager tool on my phone (where I'm testing my app). Is there any way to fix this - either by deleting those empty files or by having the intent ignore them?

Note: I don't know if this is helpful, but the images above are generated by my app, which uses Camera to capture and save images and later load them back through the file picker. The image with the preview is visible in the File Manager and is not 0 B, but the others are invisible.

Screenshot of file picker

Edit: Here's the code I'm using to capture images using camera and save them.

public void captureImageAction()
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_DENIED ||
                    checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {

                String[] permission = {Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE};

                requestPermissions(permission, PERMISSION_CODE);
            } else {
                openCamera();
            }
        } else {
            openCamera();
        }
    }

    private void openCamera() {
        ContentValues values = new ContentValues();
        values.put(MediaStore.Images.Media.TITLE, "New Picture");
        values.put(MediaStore.Images.Media.DESCRIPTION, "From Camera");

        image_uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, image_uri);
        int IMAGE_CAPTURE_CODE = 1001;
        startActivityForResult(cameraIntent, IMAGE_CAPTURE_CODE);
    }
rahul98
  • 1
  • 1
  • No you will see them. But who created these files? An app of you using a camera intent? – blackapps Jun 19 '20 at 11:44
  • Of course you could try to delete those files. – blackapps Jun 19 '20 at 11:45
  • @blackapps I don't know how the files got there, but I don't want the user to see them while picking files. I don't know how to delete those files, or if they even exist in the first place. As I mentioned, they are not visible in my file manager. My app uses camera intent, so maybe it is the one creating those files, but I do not know for sure – rahul98 Jun 19 '20 at 11:49
  • Is your app using something like createImageFile with the camera intent? If so then that is it. Use a different file manager to show all files. – blackapps Jun 19 '20 at 12:47
  • @blackapps No, I'm not using createImageFile or any functions other than the ones I indicated above. As for file managers, I checked with 3 different file managers, and none of them could see the files listed as 0 B in the file picker, which leads me to believe that those files don't exist – rahul98 Jun 19 '20 at 15:16
  • `image_uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI` Do not execute the code after this line and just check if an empty file exists. You could also start the camera and then cancel the operation. Check if an empty file existt. If you take a regcognisable name for the file you quickly see if it is you that is doing it. – blackapps Jun 19 '20 at 15:23
  • Maybe only the mediastore knows about it because of the insert() done. – blackapps Jun 19 '20 at 15:24

0 Answers0