if (resultCode == Activity.RESULT_OK && requestCode == 1 && null != data) { Uri selectedImage = data.getData(); InputStream imageStream =getActivity().getContentResolver().openInputStream(selectedImage); System.out.println("dfsdf"); Bitmap bitmap2 = BitmapFactory.decodeStream(imageStream);
基本上是onactivityresult这就是我如何阅读,我得到选定的图像为null。当我从文件管理器(/sdcard)中选择我的图像时......当我从uhf播放器中选择..我从相机或屏幕截图中选择时,它工作正常
Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult( Intent.createChooser(intent, "Select Picture"), 1);
答案 0 :(得分:2)
//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"/>
答案 1 :(得分:0)
使用此代码启动意向选择器 -
Intent pickIntent = new Intent();
pickIntent.setType("image/*");
pickIntent.setAction(Intent.ACTION_GET_CONTENT);
Intent takePhotoIntent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
String pickTitle = this
.getResources()
.getString(R.string.edit_general_select_or_take_picture); // Or
// get
// from
// strings.xml
Intent chooserIntent = Intent.createChooser(pickIntent,
pickTitle);
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
new Intent[] { takePhotoIntent });
startActivityForResult(chooserIntent,
SELECT_PICTURE);
你的onActivityResult会是这样的 -
protected void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
if (imageReturnedIntent != null) {
if (imageReturnedIntent.getData() != null) {
Uri selectedImage = imageReturnedIntent.getData();
}
}
}
希望这有帮助。