zoukankan      html  css  js  c++  java
  • Android实现拍照与打开本地图片

    代码如下:

    publicclass MainActivity extends Activity {

        private Button btnCamera;

        private Button btnLocalPic;

        private ImageView imageView;

        @Override

        protectedvoid 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

                publicvoid 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

                publicvoid 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

        protectedvoid 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);

            } elseif (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();

                }

            }

        }

    }

  • 相关阅读:
    【leetcode】1295. Find Numbers with Even Number of Digits
    【leetcode】427. Construct Quad Tree
    【leetcode】1240. Tiling a Rectangle with the Fewest Squares
    【leetcode】1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold
    【leetcode】1291. Sequential Digits
    【leetcode】1290. Convert Binary Number in a Linked List to Integer
    【leetcode】1269. Number of Ways to Stay in the Same Place After Some Steps
    【leetcode】1289. Minimum Falling Path Sum II
    【leetcode】1288. Remove Covered Intervals
    【leetcode】1287. Element Appearing More Than 25% In Sorted Array
  • 原文地址:https://www.cnblogs.com/Yellow0-0River/p/4240617.html
Copyright © 2011-2022 走看看