Cdacians

Cdacians
Cdacians

Tuesday 7 August 2012

Save the captured canvas bitmap from custom View


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.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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();
      
 }
  
 
}

akm
www.cdacians.com

No comments:

Post a Comment