Android 获取照相机图片或本地图片

网上找了一些代码,我再小米手机上测试,发现还是有的不是很好用,所以找了资料,把代码贴出来分享一下。

[java][/java] view plaincopy
  1. public final static int CONSULT_DOC_PICTURE = 1000;
  2. public final static int CONSULT_DOC_CAMERA = 1001;
  3. private int SELECT_PICTURE = 0;
  4. private int SELECT_CAMERA = 1;
  5. private ImageView iv;
  6. private Bitmap bmp;
  7. private Uri outputFileUri;
[java][/java] view plaincopy
  1. private OnClickListener cameraClickListener = new OnClickListener() {
  2.         @Override
  3.         public void onClick(View v) {
  4.             // TODO Auto-generated method stub
  5.             CharSequence[] items = { “相册”, “相机” };
  6.             new AlertDialog.Builder(getActivity()).setTitle(“选择图片来源”).setItems(items, new DialogInterface.OnClickListener() {
  7.                 public void onClick(DialogInterface dialog, int which) {
  8.                     if (which == SELECT_PICTURE) {
  9.                         Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
  10.                         intent.addCategory(Intent.CATEGORY_OPENABLE);
  11.                         intent.setType(“image/*”);
  12.                         startActivityForResult(Intent.createChooser(intent, “选择图片”), CONSULT_DOC_PICTURE);
  13.                     } else {
  14.                         File file = new File(Environment.getExternalStorageDirectory(), “textphoto.jpg”);
  15.                         outputFileUri = Uri.fromFile(file);
  16.                         Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  17.                         intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
  18.                         startActivityForResult(intent, CONSULT_DOC_CAMERA);
  19.                     }
  20.                 }
  21.             }).create().show();
  22.         }
  23.     };
[java][/java] view plaincopy
  1. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  2.         super.onActivityResult(requestCode, resultCode, data);
  3.         if (requestCode == ConsultDoctor.CONSULT_DOC_PICTURE) {
  4.             if(data == null){
  5.                 return;
  6.             }
  7.             Uri uri = data.getData();
  8.             String[] proj = { MediaStore.Images.Media.DATA };
  9.             Cursor cursor = getActivity().managedQuery(uri, proj, // Which
  10.                                                                     // columns
  11.                                                                     // to return
  12.                     null, // WHERE clause; which rows to return (all rows)
  13.                     null, // WHERE clause selection arguments (none)
  14.                     null); // Order-by clause (ascending by name)
  15.             int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
  16.             cursor.moveToFirst();
  17.             String path = cursor.getString(column_index);
  18.             if (bmp != null)// 如果不释放的话,不断取图片,将会内存不够
  19.                 bmp.recycle();
  20.             bmp = BitmapFactory.decodeFile(path);
  21.             iv.setImageBitmap(bmp);
  22.         } else if (requestCode == ConsultDoctor.CONSULT_DOC_CAMERA) {
  23.             bmp = BitmapFactory.decodeFile(outputFileUri.getPath());
  24.             iv.setImageBitmap(bmp);
  25.         } else {
  26.             Toast.makeText(getActivity(), “请重新选择图片”, Toast.LENGTH_SHORT).show();
  27.         }
  28.     }

我的代码是写在Fragment里面的。so…  你们懂的

标签