Android简单的调用系统相机和相册
publicvoidreasonAdd(Viewv){finalString[]strs=newString[]{"拍照","相册"};AlertDialog.Builderbuilder=newAlertDialog.Builder(this);builder.setTitle("照片");builder.setItems(strs,newDialogInterface.OnClickListener(){@OverridepublicvoidonClick(DialogInterfacedialog,intwhich){//拍照可以用两种方法来实现//1.调用系统相机2.自定义相机if(which==0){Intentintent=newIntent(MediaStore.ACTION_IMAGE_CAPTURE);startActivityForResult(intent,1);}//调用系统相册if(which==1){Intentintent=newIntent(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();}//写一个方法来实现相机@OverrideprotectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){super.onActivityResult(requestCode,resultCode,data);if(resultCode==Activity.RESULT_OK){StringsdStatus=Environment.getExternalStorageState();if(!sdStatus.equals(Environment.MEDIA_MOUNTED)){Log.i("TestFile","SDcardisnotavaiablerightnow");return;}Stringname=Calendar.getInstance(Locale.CANADA)+".jpg";Bundlebundle=data.getExtras();FileOutputStreamb=null;//实现设置图片的大小,然后显示Intentintent1=newIntent("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);Bitmapsource=(Bitmap)bundle.get("data");file=newFile("/sdcard/myp_w_picpath/");file.mkdir();StringFilename="/sdcard/myp_w_picpath/"+name;try{b=newFileOutputStream(Filename);source.compress(Bitmap.CompressFormat.JPEG,100,b);b.flush();b.close();}catch(FileNotFoundExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}leave_iv_icon.setImageBitmap(source);icon=source;}elseif(resultCode==0){BitmapcameraBitmap=(Bitmap)data.getExtras().get("data");leave_iv_icon.setImageBitmap(cameraBitmap);icon=cameraBitmap;}}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。