首页 > 代码库 > 调取系统相机、并将图片展示在ImageView上

调取系统相机、并将图片展示在ImageView上

+ View Code?
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
<span style="color: rgb(0, 0, 255); font-size: 15px;"><strong>package com.ting.textcover;
 
import java.io.File;
 
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.SystemClock;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
 
/**
 *   调取系统相机、并将图片展示在ImageView上
 * @author siva
 *
 */
public class MainActivity extends Activity {
 
     
    private Button btn ;
    private ImageView image;
    private String pathName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         
        btn = (Button) findViewById(R.id.btn);
        image = (ImageView) findViewById(R.id.iamgeView);
        pathName = Environment.getExternalStorageDirectory()+"/image.jpg";
        btn.setOnClickListener(btnlistener);
         
    }
    private OnClickListener btnlistener = new OnClickListener() {
         
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            //用户设置一个临时保存文件的地址,每次都会被替换掉
            Uri newFileUri = Uri.fromFile(new File(pathName));
            intent.putExtra(MediaStore.EXTRA_OUTPUT, newFileUri);
            startActivityForResult(intent, 1);
        }
    };
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
     
        if (resultCode==RESULT_OK) {
            switch (requestCode) {
            case 1:
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true//这时候decode的bitmap为null,只是宽高放到其中
                Bitmap bitmap = BitmapFactory.decodeFile(pathName,options);
                options.inJustDecodeBounds = false;
                //因为是等比例缩放、因此只设置一个即可
                int be = options.outHeight/300;
                if(be<=0){
                    be=1;
                }
                options.inSampleSize = be;  //缩放比例
                //重新读入图片,此时的bitmap不是null值;若此处出现null ,查看pathname是否有问题
                bitmap = BitmapFactory.decodeFile(pathName,options);
                int withd = bitmap.getWidth();
                int height = bitmap.getHeight();
                System.out.println(withd+"===="+height);
                image.setImageBitmap(bitmap);
                // 将图片保存在SD卡上
                break;
            default:
                break;
            }
        }
     
    }
         
     
     
     
}
</strong></span>