简体   繁体   English

Android如何使用一个Intent同时从相机或图库中选择图像

[英]Android how can I pick image from camera or gallery at the same time with one Intent

Here's a implementation I found in a app named Pawoo . 这是我在名为Pawoo的应用中找到的实现。 I can choose take photo or pick a image from system built-in gallery or third-party gallery at the same time. 我可以同时选择拍照或从系统内置画廊或第三方画廊中选择图像。

I wonder how to achieve it with just one Intent. 我不知道如何仅凭一种意图来实现它。 Because it seems not implements by third-party library. 因为它似乎不是由第三方库实现的。

在此处输入图片说明

It's not one Intent . 这不是一个Intent This dialog is a bottom sheet . 此对话框是一个底页

I already know how to achieve it. 我已经知道如何实现它。 Inspired by Intent to choose between the camera or the gallery in Android Intent启发, 可以在Android的相机或图库之间进行选择

The answer of qustion is not just one Intent. 问题的答案不仅仅是一个意图。 Simply, in my question screentshot, there's 3 actions, that's means 3 Intents. 简单来说,在我的问题截屏中,有3个动作,即3个Intent。 The key method is Intent.createChooser() 关键方法是Intent.createChooser()

Here's my complete code: 这是我完整的代码:

public void click(View view) {
        File file = getExternalFilesDir(Environment.DIRECTORY_DCIM);
        Uri cameraOutputUri = Uri.fromFile(file);
        Intent intent = getPickIntent(cameraOutputUri);
        startActivityForResult(intent, -1);
    }

    private Intent getPickIntent(Uri cameraOutputUri) {
        final List<Intent> intents = new ArrayList<Intent>();

        if (true) {
            intents.add(new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI));
        }

        if (true) {
            setCameraIntents(intents, cameraOutputUri);
        }

        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 = 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);
        }
    }

Here's the my demo: 这是我的演示: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM