//Here is some sample code to pick photo from gallery or get from camera.
//Declare the following
private static final int SELECT_PHOTO = 100;
private static final int CAMERA_REQUEST=101;
//way to call startactivityforresult select photo from gallery(sd card)
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
//way to call startactivityforresult select photo from camera
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
//onActivityResult method
@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);
}
}
}
//at last use this for camera use in your Manifest file
<uses-permission android:name="android.permission.CAMERA"/>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…