Search code examples
androidandroid-gallery

How to save a bitmap image with imageview onclick


Am developing a coloring of images in android. So after applying colors to my image when i click another imageview like save, then i have to save that image to gallery.


Solution

  • To get Bitmap from imageView:

    imageview.buildDrawingCache();
    Bitmap bm=imageview.getDrawingCache();
    

    To save it in a file:

    OutputStream fOut = null;
    Uri outputFileUri;
    try {
        File root = new File(Environment.getExternalStorageDirectory()
            + File.separator + "folder_name" + File.separator);
        root.mkdirs();
        File sdImageMainDirectory = new File(root, "myPicName.jpg");
        outputFileUri = Uri.fromFile(sdImageMainDirectory);
        fOut = new FileOutputStream(sdImageMainDirectory);
    } catch (Exception e) {
        Toast.makeText(this, "Error occured. Please try again later.",
        Toast.LENGTH_SHORT).show();
    }
    try {
        bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
        fOut.flush();
        fOut.close();
    } catch (Exception e) {
    }