Skip to content Skip to sidebar Skip to footer

File.exists() Returns False For Existing File In Android

In my app user can select image from sdcard and set as profile picture. Everything is working fine but when user selects image from whatsapp folder from sdcard image can not decode

Solution 1:

I was modifying image of other apps. I think this might be the problem. So what I did ?

  1. select image and get path in onActivityResult()
  2. copy image from this path in temp file using below code
  3. use temp file for cropping and other processing

privatevoidcopyFile(File sourceFile, File destFile)throws IOException {
    if (!sourceFile.exists()) {
        return;
    }

    FileChannelsource=null;
    FileChanneldestination=null;
    source = newFileInputStream(sourceFile).getChannel();
    destination = newFileOutputStream(destFile).getChannel();
    if (destination != null && source != null) {
        destination.transferFrom(source, 0, source.size());
    }
    if (source != null) {
        source.close();
    }
    if (destination != null) {
        destination.close();
    }

}

Hope this may help someone else.

Solution 2:

If your ImageFile.exists() method is giving false in android, but it exists in memory, then you definitely have not given Write-external-storage Permission in the Manifest file of Your Project. Add this Permission in the Manifest of Your Project:

<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Post a Comment for "File.exists() Returns False For Existing File In Android"