//返回码 :本地图库 private static final int RESULT_IMAGE = 100; //返回码 :相机 private static final int RESULT_CAMERA = 200; //IMAGE TYPE private static final String IMAGE_TYPE = "image/*"; //Temp照片路径public static String TEMP_IMAGE_PATH;
//拍照
TEMP_IMAGE_PATH = Environment.getExternalStorageDirectory().getPath()+"/temp.png"; Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); Uri photoUri = Uri.fromFile(new File(TEMP_IMAGE_PATH)); intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri); startActivityForResult(intent,RESULT_CAMERA);//相册Intent intent = new Intent(Intent.ACTION_PICK,null); intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,IMAGE_TYPE); startActivityForResult(intent,RESULT_IMAGE);@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(resultCode == RESULT_OK){ if(requestCode == RESULT_IMAGE && data != null){ //相册 Cursor cursor = this.getContentResolver().query(data.getData(),null,null,null,null); cursor.moveToFirst(); String imagePath = cursor.getString(cursor.getColumnIndex("_data")); cursor.close(); } else if(requestCode == RESULT_CAMERA){ //相机 图片保存到Uri中 } } }
Android拍照与相册选取精简
最新推荐文章于 2022-02-21 13:28:26 发布