String chooseTitle = activity.getString(R.string.select_or_take_picture);
Intent getIntent = new Intent();
getIntent.setType("image/*");
getIntent.setAction(Intent.ACTION_GET_CONTENT);
Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
galleryIntent.setType("image/*");
PackageManager pm = activity.getApplicationContext().getPackageManager();
for (ResolveInfo ri: pm.queryIntentActivities(galleryIntent, PackageManager.MATCH_DEFAULT_ONLY)) {
Intent intent = pm.getLaunchIntentForPackage(ri.activityInfo.packageName);
intent.setAction(Intent.ACTION_PICK);
intents.add(intent);
}
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE, photoUri);
for (ResolveInfo ri : pm.queryIntentActivities(cameraIntent, 0)) {
Intent intent = pm.getLaunchIntentForPackage(ri.activityInfo.packageName);
intents.add(intent);
}
Intent chooserIntent = Intent.createChooser(getIntent, chooseTitle);
chooserIntent.putExtra(
Intent.EXTRA_INITIAL_INTENTS,
intents.toArray(new Parcelable[] {})
);
通过这种方式,选择器会显示:
相机 Intent 根本不显示。
但是如果我改变线路
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE, photoUri);
至
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
它工作得很好:
但问题是,我真的想传递photoUri
。我该如何处理它?</p>
我知道一个可能的替代方案是编写自己的选择器对话框,但我确实想知道这是否是 Intent 选择器中的错误,或者我是否没有正确使用它。
附: @dkarmazi,这是我生成 Uri 的方法:
public Uri generatePhotoUri() {
String timeStamp = new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date());
String imageFileName = "XXX_" + timeStamp + ".jpg";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File imageFile = new File(storageDir, imageFileName);
return Uri.fromFile(imageFile);
}
这是我的onActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case PICK_PHOTO:
if (resultCode != Activity.RESULT_OK) {
break;
}
Uri source = data == null ?
mPhotoUri : // take picture
data.getData(); // choose from other app
if (source == null) {
break;
}
// TODO: do with source
break;
}
//....
}
@dkarmazi,我已经对其进行了调试,并且确保 resultCode 为 RESULT_CANCELED
。
最佳答案
我最近解决了同样的问题,这是我的解决方案:
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
然后,一旦用户拍照,您应该能够使用提供的 photoUri 访问它
这是关于 EXTRA_OUTPUT 的一些文档您还可以在同一页面上查找 ACTION_IMAGE_CAPTURE。
Intent 选择器更新:
// we create intent chooser by picking one of the intents
Intent chooserIntent = Intent.createChooser(cameraIntent, getResources().getString(R.string.pick_action_string_for_user));
// then we add any additional intents
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { getIntent });
// chooserIntent is ready
startActivityForResult(chooserIntent, requestCode);
关于android - 相机 Intent 未显示在 Intent 选择器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30388523/