Android - Save images in an specific folder

后端 未结 5 1492
执笔经年
执笔经年 2020-11-29 22:01

I need to save the pictures taken with my app in an specific folder. I\'ve read many solutions to this problem but I couldn\'t make any of them work so I ask for help.

5条回答
  •  一整个雨季
    2020-11-29 22:16

    Go through the following code , its working fine for me.

    private void createDirectoryAndSaveFile(Bitmap imageToSave, String fileName) {
    
        File direct = new File(Environment.getExternalStorageDirectory() + "/DirName");
    
        if (!direct.exists()) {
            File wallpaperDirectory = new File("/sdcard/DirName/");
            wallpaperDirectory.mkdirs();
        }
    
        File file = new File("/sdcard/DirName/", fileName);
        if (file.exists()) {
            file.delete();
        }
        try {
            FileOutputStream out = new FileOutputStream(file);
            imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

提交回复
热议问题