选择器具有相机意图和图像选择器意图 [英] Chooser with camera intent and image picker intent

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

问题描述

我创建了一个选择器,可以从文件中选择图片或创建图片。

I have created a chooser for either picking an image from file or for making a picture.

我使用的代码在Nexus 5上正常工作,尝试它在三星S5,选择器不显示相机图标。

The code I use works fine on a Nexus 5, however when I try it on a Samsung S5, the chooser does not display the camera icons.

public Intent makePhotoIntent(String title, Context ctx, String uniqueImageId){

        //Build galleryIntent
        Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        galleryIntent.setType("image/*");

        //Create chooser
        Intent chooser = Intent.createChooser(galleryIntent,title);

        if (checkexCameraHardware()){
            Intent  cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            mTempImage = null;
            try {
                mTempImage = createImageFile(uniqueImageId);
            } catch (IOException e) {
                e.printStackTrace();
            }

            if (mTempImage != null){
                cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(mTempImage)); //add file ure (photo is saved here)
                Intent[] extraIntents = {cameraIntent};
                chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
            }
        }
        return chooser;
    }

当我更改意图添加到选择器的顺序时,Samsung设备显示相机,但只显示android-system作为文件选项。

When I change the order in which the intents are added to the chooser the Samsung device does show the camera but only shows android-system as the file option.

public Intent makePhotoIntent(String title, Context ctx, String uniqueImageId){

        //Build galleryIntent
        Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        galleryIntent.setType("image/*");

        //Create chooser
        Intent chooser = Intent.createChooser(galleryIntent,title);

        if (checkexCameraHardware()){
            Intent  cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            mTempImage = null;
            try {
                mTempImage = createImageFile(uniqueImageId);
            } catch (IOException e) {
                e.printStackTrace();
            }

            if (mTempImage != null){
                cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(mTempImage)); //add file ure (photo is saved here)

                //I have to re-create the chooser here or the Samsung will not show the 'camera' icons.
                //I have to add the cameraIntent first.
                chooser = Intent.createChooser(cameraIntent,title);
                Intent[] extraIntents = {galleryIntent};

                //Intent[] extraIntents = {cameraIntent};
                chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
            }
        }
        return chooser;
    }

理想情况下,我希望三星设备显示相同的Nexus一个,当我添加图库目的首先。

Ideally I would like the Samsung device to show the same as the Nexus one when I add the gallery intent first. I can however not get this to work.

推荐答案


  • 如果有多个意向匹配对于额外的意图( Intent.EXTRA_INITIAL_INTENTS ),它会在Android系统下显示它们的所有组合。当你点击Android系统,它会打开另一个选择器与所有这些意图匹配。

    • If there are multiple intent matches for the extra intent (Intent.EXTRA_INITIAL_INTENTS) , it will show all of them combined under "Android System". When you click on "Android System", it will open another chooser with all those intent matches.

      因此,在samsung设备的第一个屏幕截图,

      So, in your first screenshot for samsung device, the camera is indeed showing up - its just under "Android Systeem" (clicking this will show all camera intent matches).

      如果您创建,则会显示Android Systeem galleryIntent 多余的而不是相机的意图,它将结合所有图库相关的意图下Android Systeem(如你在第二个屏幕截图中看到三星设备)

      If you make galleryIntent the extra instead of camera intent, it will combine all gallery related intents under "Android Systeem" (as seen in your 2nd screenshot for samsung device)

      我想如果他不知道这个Android系统是什么,用户可能会感到困惑!

      I guess the user can be confused if he does not know what this "Android Systeem" is !


      • 使用以下解决方案,您可以直接将每个添加到 chooser

      在您的第二个代码段中,替换

      In your 2nd code snippet, replace

      Intent[] extraIntents = {galleryIntent};
      chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
      

      其中:

      List<Intent> galleryIntents = new ArrayList<Intent>();
      PackageManager pm = getApplicationContext().getPackageManager();
      for (ResolveInfo ri: pm.queryIntentActivities(galleryIntent, PackageManager.MATCH_DEFAULT_ONLY)) {
          Intent intent = pm.getLaunchIntentForPackage(ri.activityInfo.packageName);
          intent.setAction(Intent.ACTION_PICK);
          galleryIntents.add(intent); 
      }
      chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, galleryIntents.toArray(new Parcelable[] {}));
      


    • 您的 chooser

      这篇关于选择器具有相机意图和图像选择器意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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