file.exists() returns false for existing file in android

后端 未结 2 1784
余生分开走
余生分开走 2021-01-24 23:42

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 dec

2条回答
  •  死守一世寂寞
    2021-01-25 00:05

    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

    private void copyFile(File sourceFile, File destFile) throws IOException {
        if (!sourceFile.exists()) {
            return;
        }
    
        FileChannel source = null;
        FileChannel destination = null;
        source = new FileInputStream(sourceFile).getChannel();
        destination = new FileOutputStream(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.

提交回复
热议问题