1 package cn.itcast.bitmapcopy;
2
3 import java.io.FileNotFoundException;
4
5 import android.app.Activity;
6 import android.content.Intent;
7 import android.graphics.Bitmap;
8 import android.graphics.BitmapFactory;
9 import android.graphics.Canvas;
10 import android.graphics.Matrix;
11 import android.graphics.Paint;
12 import android.net.Uri;
13 import android.os.Bundle;
14 import android.view.View;
15 import android.widget.ImageView;
16
17 public class DemoActivity extends Activity {
18 private ImageView iv,iv_copy;
19
20 @Override
21 public void onCreate(Bundle savedInstanceState) {
22 super.onCreate(savedInstanceState);
23 setContentView(R.layout.main);
24 iv = (ImageView) findViewById(R.id.iv);
25 iv_copy = (ImageView) findViewById(R.id.iv_copy);
26
27 }
28
29
30 public void selectImage(View view){
31 Intent intent = new Intent();
32 intent.setAction("android.intent.action.PICK");
33 intent.addCategory("android.intent.category.DEFAULT");
34 intent.setType("image/*");
35 startActivityForResult(intent, 0);
36 }
37
38
39 @Override
40 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
41 try {
42 if (data != null) {
43 // 得到点击图片的uri44 Uri uri = data.getData();
45 //原图46 Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
47 iv.setImageBitmap(bitmap);
48 //原图的拷贝49 Bitmap alterBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getWidth(), bitmap.getConfig());
50 //以拷贝图片的为模板创建一个画布51 Canvas canvas = new Canvas(alterBitmap);
52 canvas.drawBitmap(bitmap, new Matrix(), new Paint());
53 iv_copy.setImageBitmap(alterBitmap);
54 }
55 } catch (FileNotFoundException e) {
56 e.printStackTrace();
57 }
58 super.onActivityResult(requestCode, resultCode, data);
59 }
60 }