May 2, 2012

Save the captured canvas bitmap from custom View


Last article demonstrated how to get the canvas bitmap of custom view. In this article, I will show how to save in file.

Save the captured canvas bitmap from custom View


Modify the openCaptureDialog() method in main activity(AndroidMyCanvasActivity.java) of last article to save the bitmap in jpg. It will be saved in root of sdcard, named test.jpg.

package com.AndroidMyCanvas;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import android.app.Activity;
import android.app.AlertDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class AndroidMyCanvasActivity extends Activity {
 
 MyView myView;
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        myView = new MyView(this);

        setContentView(myView);
        
    }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  MenuInflater menuInflater = getMenuInflater();
  menuInflater.inflate(R.menu.menu, menu);
  return true;
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  switch(item.getItemId()){
  case R.id.opt_capture:
   openCaptureDialog();
   return true;
  default:
   return false;
  
  }
 }
 
 private void openCaptureDialog(){

  Bitmap bmMyView = myView.getCanvasBitmap();
  
  //Save in file
  String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
  OutputStream outStream = null;
  File file = new File(extStorageDirectory, "test.jpg");
  
  try {
   outStream = new FileOutputStream(file);
   bmMyView.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
   
   outStream.flush();
      outStream.close();
      Toast.makeText(AndroidMyCanvasActivity.this, "Saved", Toast.LENGTH_LONG).show();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   Toast.makeText(AndroidMyCanvasActivity.this, e.toString(), Toast.LENGTH_LONG).show();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   Toast.makeText(AndroidMyCanvasActivity.this, e.toString(), Toast.LENGTH_LONG).show();
  }
  
  //

  AlertDialog.Builder captureDialog = new AlertDialog.Builder(AndroidMyCanvasActivity.this);
  captureDialog.setTitle("Canvas Captured");
  
  ImageView bmImage = new ImageView(AndroidMyCanvasActivity.this);
  
     bmImage.setImageBitmap(bmMyView);
     
     LayoutParams bmImageLayoutParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
     bmImage.setLayoutParams(bmImageLayoutParams);
      
     LinearLayout dialogLayout = new LinearLayout(AndroidMyCanvasActivity.this);
     dialogLayout.setOrientation(LinearLayout.VERTICAL);
     dialogLayout.addView(bmImage);
     captureDialog.setView(dialogLayout);
     
  captureDialog.setPositiveButton("OK", null);
  captureDialog.show();
     
 }
 

}


1 comment:

  1. Nice. A good addition would be a simple load/restore bitmap from file. I saw that later in the series you have a more complex jpeg chooser and loader, but a really simple version like the save bitmap above would be useful at this point. It could be used to restore the view/bitmap when for instance the orientation is changed or the app was paused/resumed.

    ReplyDelete

Infolinks In Text Ads