Android How can I call Camera or Gallery Intent Together

后端 未结 7 1788
被撕碎了的回忆
被撕碎了的回忆 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:01

    If you want to show all the apps installed in the phone that can deal with photos such as Camera, Gallery, Dropbox, etc.

    You can do something like:

    1.- Ask for all the intents available:

        Intent camIntent = new Intent("android.media.action.IMAGE_CAPTURE");
        Intent gallIntent=new Intent(Intent.ACTION_GET_CONTENT);
        gallIntent.setType("image/*"); 
    
        // look for available intents
        List info=new ArrayList();
        List yourIntentsList = new ArrayList();
        PackageManager packageManager = context.getPackageManager();
        List listCam = packageManager.queryIntentActivities(camIntent, 0);
        for (ResolveInfo res : listCam) {
            final Intent finalIntent = new Intent(camIntent);
            finalIntent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
            yourIntentsList.add(finalIntent);
            info.add(res);
        }
        List listGall = packageManager.queryIntentActivities(gallIntent, 0);
        for (ResolveInfo res : listGall) {
            final Intent finalIntent = new Intent(gallIntent);
            finalIntent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
            yourIntentsList.add(finalIntent);
            info.add(res);
        }
    

    2.- Show a custom dialog with the list of items:

        AlertDialog.Builder dialog = new AlertDialog.Builder(context);
        dialog.setTitle(context.getResources().getString(R.string.select_an_action));
        dialog.setAdapter(buildAdapter(context, activitiesInfo),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        Intent intent = intents.get(id);
                        context.startActivityForResult(intent,1);
                    }
                });
    
        dialog.setNeutralButton(context.getResources().getString(R.string.cancel),
                new android.content.DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        dialog.show();
    

    This is a full example: https://gist.github.com/felixgborrego/7943560

提交回复
热议问题