码迷,mamicode.com
首页 > 移动开发 > 详细

Android实现拍照与打开本地图片

时间:2015-01-22 08:16:21      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:

代码如下:

public class MainActivity extends Activity {

????private Button btnCamera;

????private Button btnLocalPic;

????private ImageView imageView;

?

????@Override

????protected void onCreate(Bundle savedInstanceState) {

????????// TODO Auto-generated method stub

????????super.onCreate(savedInstanceState);

????????setContentView(R.layout.mainactivity);

????????btnCamera = (Button) this.findViewById(R.id.btnCamera);

????????btnLocalPic = (Button) this.findViewById(R.id.btnlocalPic);

????????imageView = (ImageView) this.findViewById(R.id.imageView1);

?

????????btnCamera.setOnClickListener(new OnClickListener() {

?

????????????@Override

????????????public void onClick(View arg0) {

????????????????// TODO Auto-generated method stub

????????????????Intent intent = new Intent(

????????????????????????android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

????????????????startActivityForResult(intent, 1000);

????????????}

????????});

?

????????btnLocalPic.setOnClickListener(new OnClickListener() {

?

????????????@Override

????????????public void onClick(View arg0) {

????????????????// TODO Auto-generated method stub

????????????????Intent intent = new Intent(Intent.ACTION_GET_CONTENT);

????????????????intent.setType("image/*");

????????????????intent.putExtra("crop", true);

????????????????intent.putExtra("return-data", true);

????????????????startActivityForResult(intent, 1001);

????????????}

????????});

????}

?

????@Override

????protected void onActivityResult(int requestCode, int resultCode, Intent data) {

????????// TODO Auto-generated method stub

????????super.onActivityResult(requestCode, resultCode, data);

????????if (requestCode == 1000 && resultCode == RESULT_OK) {

????????????Bundle bundle = data.getExtras();

????????????Bitmap bm = (Bitmap) bundle.get("data");

????????????imageView.setImageBitmap(bm);

????????} else if (requestCode == 1001 && resultCode == RESULT_OK) {

????????????Uri uri = data.getData();

????????????ContentResolver contentResolver = getContentResolver();

????????????try {

????????????????Bitmap bm = BitmapFactory.decodeStream(contentResolver

????????????????????????.openInputStream(uri));

????????????????imageView.setImageBitmap(bm);

????????????} catch (Exception e) {

????????????????// TODO: handle exception

????????????????e.printStackTrace();

????????????}

????????}

????}

}

Android实现拍照与打开本地图片

标签:

原文地址:http://www.cnblogs.com/Yellow0-0River/p/4240617.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!