我试图通过以下代码初始化图像选择器对话框: Intent chooserIntent = null;
List<Intent> intentList = new ArrayList<>();
Intent pickIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePhotoIntent.putExtra("return-data", true);
takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile(context)));
intentList = addIntentsToList(context, intentList, pickIntent);
intentList = addIntentsToList(context, intentList, takePhotoIntent);
if (intentList.size() > 0) {
chooserIntent = Intent.createChooser(intentList.remove(intentList.size() - 1), "");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentList.toArray(new Parcelable[intentList.size()]));
}
startActivityForResult(chooserIntent, PICK_IMAGE_ID);
但是
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode) {
case PICK_IMAGE_ID:
Bitmap bitmap = ImagePicker.getImageFromResult(this, resultCode, data);
currentImageButton.setImageDrawable(new BitmapDrawable(bitmap));
break;
default:
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
我得到零意图和-1 resultCode
我已经添加了明确的权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
代码在大多数设备上运行良好但在三星二重奏上失败。哪里错了?
UPD
答案 0 :(得分:3)
没关系,您正在获取-1
- 这意味着public static final int RESULT_OK
(请参阅Activity
课程)。
Null
作为意图意味着您已在此处定义了输出uri:
i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile(context)));
(即你可以使用Uri
,一旦得到确认,就完成了捕获)
关于图像选择,我是这样做的(它与Google在Taking Photos Simply文章中建议的几乎相同):
.....
private Uri capturedImageURI;
.....
capturedImageURI = Uri.fromFile(createImageFile());
final Intent captureIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, capturedImageURI);
Intent chooserIntent = Intent.createChooser(new Intent(Intent.ACTION_PICK).setType("image/*"), "Select Picture");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Parcelable[] { captureIntent });
startActivityForResult(chooserIntent, 42);
.....
public File createImageFile() {
File imageStorageDir = new File(
Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES)
, "MyAppPictures");
if (!imageStorageDir.exists()) {
imageStorageDir.mkdirs();
}
return new File(
imageStorageDir + File.separator + "IMG_"
+ String.valueOf(System.currentTimeMillis())
+ ".jpg");
}
.....
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
if (data != null) {
capturedImageURI = data.getData();
}
((ImageView)findViewById(R.id.capturedImageView)).setImageURI(capturedImageURI);
}
}
NB!您需要这些权限才能使其正常运行:
<uses-feature android:name="android.hardware.camera"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
在 Android 6.0 中,您必须先检查权限,然后才能从外部存储中获取数据。