Save bitmap to app folder

前端 未结 5 1341
青春惊慌失措
青春惊慌失措 2020-12-15 02:03

When my app first starts, it lets the user select a profile picture. This can be done taking a photo at the moment, or selecting it from the gallery.

After the user

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-15 02:58

    After trying several things, this is what finally has worked for me:

    ContextWrapper cw = new ContextWrapper(getApplicationContext());
    File directory = cw.getDir("profile", Context.MODE_PRIVATE);
    if (!directory.exists()) {
        directory.mkdir();
    }
    File mypath = new File(directory, "thumbnail.png");
    
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(mypath);
        resizedbitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.close();
    } catch (Exception e) {
        Log.e("SAVE_IMAGE", e.getMessage(), e);
    }
    

    Basically the thing is to check if the directory (not the file) exists, and if not, create it with mkdir().

提交回复
热议问题