Android How can I call Camera or Gallery Intent Together

后端 未结 7 1768
被撕碎了的回忆
被撕碎了的回忆 2020-12-02 09:27

If I want to capture image from native camera, I can do:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTR         


        
相关标签:
7条回答
  • 2020-12-02 10:07

    In fact your dialog title is "select an action" means the dialog is a Intent chooser actually. not a user customed dialog. The every single item presents a Intent.

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

    You may need solve permissions by yourself if run on OS >= 23

    Here's my demo:(Appearance differences since OS different)

    0 讨论(0)
提交回复
热议问题