用户从图库或自定义相机中选择图像,而不是图像

时间:2016-01-05 05:09:51

标签: java android android-intent android-intent-chooser

我希望用户能够点击按钮,并可以选择图库或类似应用我在应用中使用的相机活动而不是构建 - 在一个。我阅读了相关答案herehere。但是,我不想包含内置相机应用程序,但我想要包含我的相机活动。

到目前为止,我有以下设置,取自我发布的两个链接:

Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
Intent gallIntent = new Intent(Intent.ACTION_GET_CONTENT);
gallIntent.setType("image/*");

Intent camIntent = new Intent(UserEventsActivity.this,CameraActivity.class);
camIntent.setComponent(new ComponentName("MCamera", "Camera"));

List<Intent> yourIntentsList = new ArrayList<Intent>();
yourIntentsList.add(camIntent);


List<ResolveInfo> listGall = getApplicationContext().getPackageManager().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);
}


pickIntent.putExtra(Intent.EXTRA_INTENT, yourIntentsList.toArray(new Parcelable[]{}));

pickIntent.putExtra(Intent.EXTRA_TITLE, "Select Source");
startActivityForResult(pickIntent, 1);

由此产生的结果是大量不相关的应用和服务,其中大多数实际上在列表中重复了几次。另外,我的相机活动不在其中。当我做pickIntent.putExtra(Intent.EXTRA_INTENT,gallIntent)时,我确实得到了我想要的相关图库应用,但我无法添加我的活动。

关于我做错了什么的任何想法?

此外,点击它时不会打开任何服务。但是,这可能与startActivityOnResult中的'1'arg有关,因为我不确定该为arg做什么。

1 个答案:

答案 0 :(得分:0)

我提出了一个可能的解决方案,这似乎对我有用。我使用了自定义对话框:

final Dialog choosePicContent = new Dialog(UserEventsActivity.this);
            ScrollView scrollContent = new ScrollView(UserEventsActivity.this);
            choosePicContent.addContentView(scrollContent, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            LinearLayout scrollLayout = new LinearLayout(UserEventsActivity.this);
            scrollContent.addView(scrollLayout);
            scrollLayout.setOrientation(LinearLayout.VERTICAL);

            TextView title = new TextView(UserEventsActivity.this);
            title.setText(R.string.selectsource);
            scrollLayout.addView(title);
            Intent gallIntent = new Intent(Intent.ACTION_GET_CONTENT);
            gallIntent.setType("image/*");

            final Intent camIntent = new Intent(UserEventsActivity.this,CameraActivity.class);

            ImageButton camButton = new ImageButton(UserEventsActivity.this);
            camButton.setImageDrawable(getApplicationContext().getDrawable(R.drawable.ic_menu_camera));
            camButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    camIntent.putExtra("User",current_user);
                    startActivity(camIntent);
                }
            });
            scrollLayout.addView(camButton);

          List<ResolveInfo> listGall = getApplicationContext().getPackageManager().queryIntentActivities(gallIntent, 0);
            for (ResolveInfo res : listGall) {


                    final Intent finalIntent = new Intent(gallIntent);
                    finalIntent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));

                    ImageButton iconButton = new ImageButton(UserEventsActivity.this);
                    iconButton.setImageDrawable(res.activityInfo.loadIcon(getPackageManager()));
                    iconButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            startActivityForResult(finalIntent,1);
                        }
                    });
                    scrollLayout.addView(iconButton);

            }
            choosePicContent.show();