Android 获取照相机图片或本地图片
网上找了一些代码,我再小米手机上测试,发现还是有的不是很好用,所以找了资料,把代码贴出来分享一下。
public void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);if (requestCode == ConsultDoctor.CONSULT_DOC_PICTURE) {if(data == null){return;}Uri uri = data.getData();String[] proj = { MediaStore.Images.Media.DATA };Cursor cursor = getActivity().managedQuery(uri, proj, // Which// columns// to returnnull, // WHERE clause; which rows to return (all rows)null, // WHERE clause selection arguments (none)null); // Order-by clause (ascending by name)int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);cursor.moveToFirst();String path = cursor.getString(column_index);if (bmp != null)// 如果不释放的话,不断取图片,将会内存不够bmp.recycle();bmp = BitmapFactory.decodeFile(path);iv.setImageBitmap(bmp);} else if (requestCode == ConsultDoctor.CONSULT_DOC_CAMERA) {bmp = BitmapFactory.decodeFile(outputFileUri.getPath());iv.setImageBitmap(bmp);} else {Toast.makeText(getActivity(), "请重新选择图片", Toast.LENGTH_SHORT).show();}}