意图相机或Android的画廊之间进行选择 [英] Intent to choose between the camera or the gallery in Android

查看:238
本文介绍了意图相机或Android的画廊之间进行选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图发动意图从相机或Android的画廊选择一个图像。我检查<一href="http://stackoverflow.com/questions/4455558/allow-user-to-select-camera-or-gallery-for-image">THIS后,目前我的code是附近工作的:

I'm trying to launch an intent to pick a image from the camera or the android's gallery. I checked THIS post and currently my code is near to work:

private Intent getPickIntent() {
    final List<Intent> intents = new ArrayList<Intent>();
    if (allowCamera) {
        setCameraIntents(intents, cameraOutputUri);
    }
    if (allowGallery) {
        intents.add(new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI));
    }

    if (intents.isEmpty()) return null;
    Intent result = Intent.createChooser(intents.remove(0), null);
    if (!intents.isEmpty()) {
        result.putExtra(Intent.EXTRA_INITIAL_INTENTS, intents.toArray(new Parcelable[] {}));
    }
    return result;
}

private void setCameraIntents(List<Intent> cameraIntents, Uri output) {
    final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    final PackageManager packageManager = context.getPackageManager();
    final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
    for (ResolveInfo res : listCam) {
        final String packageName = res.activityInfo.packageName;
        final Intent intent = new Intent(captureIntent);
        intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
        intent.setPackage(packageName);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, output);
        cameraIntents.add(intent);
    }
}

当我设置allowCamera =真它工作正常。

When I set allowCamera=true it works correctly.

当我设置allowGallery =会显示如下选择器真:

When I set allowGallery=true it shows the following chooser:

但是,如果我设置allowCamera = true并且allowGallery =真所示的选择器是:

But if I set allowCamera=true and allowGallery =true the chooser shown is:

如果你选择 Android系统那么第一个选择器显示。

And if you select Android System then the first chooser is shown.

我想选择器是这样的:

我如何扩大的 Android系统选项?

推荐答案

在你的链接帖子你可以找到解决方案。所不同的到code是如何图库意图创建的:

In your linked post you can find the solution. The difference to your code is how the gallery intent is created:

final Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);

不具有 ACTION_PICK 你是怎么做,但与 ACTION_GET_CONTENT 。看来,如果一个 ACTION_PICK 在列表中(一个容器意图),该系统遍历它来显示挑的内容,但只要你包括摄像头的意图就无法穿越了(因为有一个直接故意和一个集装箱的意图)。

Not with ACTION_PICK how you did but with ACTION_GET_CONTENT. It seems that if a single ACTION_PICK is in the list (a "container intent"), the system traverses it to display the content of the pick, but as soon you include the camera intent it can't traverse anymore (since there is one direct intent and one container intent).

这个答案你之间找到 ACTION_PICK ACTION_GET_CONTENT

有哪些建议使用自定义对话框提供了一些解决方案。但该对话框不会有标准的图标(见develper文档这里)。因此,我建议留在您的解决方案,只是解决了hierarchie问题。

There are some solutions available which recommend to use a custom dialog. But this dialog will not have the standards icons (see develper docs here). So I recommend to stay at your solution and just fix the hierarchie issue.

这篇关于意图相机或Android的画廊之间进行选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆