gpt4 book ai didi

android - 相机 Intent 未显示在 Intent 选择器中

转载 作者:行者123 更新时间:2023-11-29 00:10:44 25 4
gpt4 key购买 nike

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[] {})
);

通过这种方式,选择器显示:

Effect when I passed photoUri

相机 Intent 根本不显示。

但是如果我换行

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE, photoUri);

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

它工作得很好:

enter image description here

但问题是,我真的很想传递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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com