Android简单的调用系统相机和相册

  public void reasonAdd(View v)
    {
       final String [] strs=new String[]{"拍照","相册"};
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("照片");
        builder.setItems(strs, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //拍照可以用两种方法来实现
                //1.调用系统相机 2.自定义相机
                if (which==0) {

                       Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        startActivityForResult(intent,1);

                }
                //调用系统相册
                if (which==1) {
                    Intent intent=new Intent(Intent.ACTION_GET_CONTENT);
                    intent.addCategory(Intent.CATEGORY_OPENABLE);
                    intent.setType("p_w_picpath/*");
                    intent.putExtra("crop", "true");
                    intent.putExtra("aspectX", 1);
                    intent.putExtra("aspectY", 1);
                    intent.putExtra("outputX", 80);
                    intent.putExtra("outputY", 80);
                    intent.putExtra("return-data", true);
                    startActivityForResult(intent, 0);
                }

            }
        });
        builder.show();
    }
     // 写一个方法来实现相机
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode== Activity.RESULT_OK)
        {

            String sdStatus= Environment.getExternalStorageState();
            if(!sdStatus.equals(Environment.MEDIA_MOUNTED))
            {
                Log.i("TestFile", "SD card is not avaiable right now");
                return;
            }
            String name= Calendar.getInstance(Locale.CANADA)+".jpg";
            Bundle bundle=data.getExtras();
            FileOutputStream b=null;
            // 实现设置图片的大小,然后显示
            Intent intent1=new Intent("com.android.camera.actioin.CROP");
            intent1.putExtra("crop","true");
            intent1.putExtra("outputX",250);
            intent1.putExtra("outputY", 250);
            intent1.putExtra("aspectX",1);
            intent1.putExtra("aspectY", 1);
            Bitmap source= (Bitmap) bundle.get("data");
            file=new File("/sdcard/myp_w_picpath/");
            file.mkdir();
            String Filename="/sdcard/myp_w_picpath/"+name;
            try {
                b=new FileOutputStream(Filename);
                source.compress(Bitmap.CompressFormat.JPEG,100,b);
                b.flush();
                b.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            leave_iv_icon.setImageBitmap(source);
            icon=source;
        }else if(resultCode==0)
        {
            Bitmap cameraBitmap = (Bitmap) data.getExtras().get("data");
            leave_iv_icon.setImageBitmap(cameraBitmap);
            icon=cameraBitmap;
        }
    }

分享名称:Android简单的调用系统相机和相册
URL网址:http://bzwzjz.com/article/poossh.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 定制级高端网站建设 网站建设推广 外贸网站建设 高端网站设计 成都网站设计 手机网站制作 成都网站制作 网站建设公司 营销网站建设 成都网站建设 网站设计制作报价 企业手机网站建设 成都网站建设 成都网站制作 重庆外贸网站建设 四川成都网站建设 成都网站制作 成都网站建设推广 成都定制网站建设 网站设计制作 网站建设方案 成都网站建设