详细解决方案
Android 之相机的使用
热度:73 发布时间:2016-05-01 10:43:20.0
Android 之照相机的使用
package?com.cloay.camera; |
003 | import?java.io.FileNotFoundException; |
005 | import?android.app.Activity; |
006 | import?android.app.AlertDialog; |
007 | import?android.content.ContentResolver; |
008 | import?android.content.Intent; |
009 | import?android.graphics.Bitmap; |
010 | import?android.graphics.BitmapFactory; |
011 | import?android.net.Uri; |
012 | import?android.os.Bundle; |
013 | import?android.view.MotionEvent; |
014 | import?android.view.View; |
015 | import?android.view.View.OnClickListener; |
016 | import?android.view.View.OnTouchListener; |
017 | import?android.widget.ImageButton; |
018 | import?android.widget.ImageView; |
021 | ?* CameraTestActivity.java |
025 | public?class?CameraTestActivity?extends?Activity { |
026 | ????private?ImageButton camera; |
027 | ????private?ImageView image; |
029 | ????public?void?onCreate(Bundle savedInstanceState) { |
030 | ????????super.onCreate(savedInstanceState); |
031 | ????????setContentView(R.layout.main); |
033 | ????????image = (ImageView) findViewById(R.id.image); |
034 | ????????camera = (ImageButton) findViewById(R.id.camera); |
035 | ????????camera.setOnTouchListener(new?OnTouchListener() { |
038 | ????????????public?boolean?onTouch(View v, MotionEvent event) { |
039 | ????????????????if(event.getAction() == MotionEvent.ACTION_DOWN){ |
040 | ????????????????????v.setBackgroundResource(R.drawable.camera_bg_pressed); |
042 | ????????????????if(event.getAction() == MotionEvent.ACTION_UP){ |
043 | ????????????????????showMenuDialog(); |
044 | ????????????????????v.setBackgroundResource(R.drawable.camera_bg_normal); |
046 | ????????????????return?false; |
051 | ????private?void?showMenuDialog() { |
052 | ????????View menuView = View.inflate(CameraTestActivity.this, R.layout.menu,?null); |
053 | ????????final?AlertDialog menuDialog =?new?AlertDialog.Builder(CameraTestActivity.this) |
054 | ????????.setView(menuView) |
055 | ????????.setTitle("选择操作") |
058 | ????????menuDialog.show(); |
059 | ????????menuView.findViewById(R.id.camera).setOnClickListener(new?OnClickListener() { |
062 | ????????????public?void?onClick(View v) { |
063 | ????????????????menuDialog.dismiss(); |
064 | ????????????????Intent intentCamera =?newIntent("android.media.action.IMAGE_CAPTURE"); |
065 | ????????????????startActivityForResult(intentCamera, Activity.DEFAULT_KEYS_DIALER); |
068 | ????????menuView.findViewById(R.id.picture).setOnClickListener(new?OnClickListener() { |
071 | ????????????public?void?onClick(View v) { |
072 | ????????????????menuDialog.dismiss(); |
088 | ?????????????????Intent intent =?new?Intent();? |
089 | ????????????????????/* 开启Pictures画面Type设定为image */? |
090 | ????????????????????intent.setType("image/*");? |
091 | ????????????????????/* 使用Intent.ACTION_GET_CONTENT这个Action */? |
092 | ????????????????????intent.setAction(Intent.ACTION_GET_CONTENT);?? |
093 | ????????????????????/* 取得相片后返回本画面 */? |
094 | ????????????????????startActivityForResult(intent,?2); |
100 | ????protected?void?onActivityResult(int?requestCode,?int?resultCode, Intent data) { |
101 | ????????if(requestCode ==?2){ |
102 | ????????????if?(resultCode == RESULT_OK) {? |
103 | ????????????????Uri uri = data.getData();? |
104 | ????????????????ContentResolver cr =?this.getContentResolver();? |
105 | ????????????????try?{? |
106 | ????????????????????Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));? |
107 | ????????????????????/* 将Bitmap设定到ImageView */? |
108 | ????????????????????image.setImageBitmap(bitmap);? |
109 | ????????????????}?catch?(FileNotFoundException e) {? |
110 | ????????????????????image.setImageResource(R.drawable.empty); |
114 | ????????if(requestCode == Activity.DEFAULT_KEYS_DIALER){ |
115 | ????????????if(data !=?null){ |
116 | ????????????????Bundle extras = data.getExtras(); |
117 | ????????????????Bitmap bitmap = (Bitmap) extras.get("data"); |
118 | ????????????????image.setImageBitmap(bitmap); |
121 | ????????????????image.setImageResource(R.drawable.empty); |
124 | ????????super.onActivityResult(requestCode, resultCode, data); |