//Here is some sample code to pick photo from gallery or get from camera.
//声明如下
private static final int SELECT_PHOTO = 100;
private static final int CAMERA_REQUEST=101;
//方法来调用startactivityforresult从图库中选择照片(SD卡)
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
//方法来调用startactivityforresult选择相机中的相片
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
// onActivityResult方法
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
if(resultCode == RESULT_OK){
//pick image from gallery(sd card)
if(requestCode==SELECT_PHOTO)
{
Uri selectedImage = imageReturnedIntent.getData();
InputStream imageStream = null;
try {
imageStream = getContentResolver().openInputStream(selectedImage);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
imageView_Babypic.setImageBitmap(yourSelectedImage);
}
//pick image from camera
else if (requestCode==CAMERA_REQUEST) {
Bitmap photo = (Bitmap) imageReturnedIntent.getExtras().get("data");
imageView_Babypic.setImageBitmap(photo);
}
}
}
//在你的清单文件一次使用这种相机使用
<uses-permission android:name="android.permission.CAMERA"/>
检查'Uri',你拿回方案。如果方案是“内容”,请使用“ContentResolver”。 – CommonsWare 2015-02-11 13:12:52
Intent intent = new Intent(Intent.ACTION_PICK, \t \t \t \t android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);如果我这样做顺便说一句,它解决了这个问题。但用户不能从SD卡中选择图像(文件管理器) – user3278732 2015-02-11 13:19:07
那么什么变成了/完全为空? – greenapps 2015-02-11 13:20:49