Search code examples
androidandroid-layoutbitmapandroid-image

Saving created Bitmap Image to internal storage


I have a basic application that uses just squares and circles that appear when the screen is touched. I want to be able to save this created image, the design is build on a bitmap image, and displayed on the canvas so I want to be able to save that as an image to the mobile device.

public MyDraw(Context context)
{   
    super(context);
    g = new Random();
    Bitmap.Config conf = Bitmap.Config.ARGB_8888;
    bmp = Bitmap.createBitmap(1100, 1800, conf);
}

Drawing code:

protected void onDraw(Canvas c)
{
    c.drawBitmap(bmp, 0, 0, paint);
    super.onDraw(c);
}

Menu Code:

public boolean onCreateOptionsMenu(Menu menu)
{
    super.onCreateOptionsMenu(menu);
    MenuItem menu1 = menu.add(0, 0, Menu.NONE, "Filled Shape");
    MenuItem menu2 = menu.add(0, 1, Menu.NONE, "Outline Shape");
    MenuItem menu3 = menu.add(0, 2, Menu.NONE, "Rectangle");
    MenuItem menu4 = menu.add(0, 3, Menu.NONE, "Oval");
    MenuItem menu5 = menu.add(0, 4, Menu.NONE, "Save!");

    return true;
}

public boolean onOptionsItemSelected(MenuItem item)
{
    switch (item.getItemId())

    {
    case 0:
        MyApp.fill = 0;
        return true;
    case 1:
        MyApp.fill = 1;
        return true;
    case 2:
        MyApp.shape = 0;
        return true;
    case 3:
        MyApp.shape = 1;
        return true;
    case 4:
        // MyDraw.bmp;
    default:
        return super.onOptionsItemSelected(item);

    }
}

I want to be able to click the save button on the menu and for the bitmap that has been created to save into the phones internal storage (preferably the gallery).


Solution

  • You need to do something like this:

    MediaStore.Images.Media.insertImage(getContentResolver(), yourBitmap, yourTitle , yourDescription);
    

    You need also to add

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

    to your manifext.xml.