1

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

到目前为止,我已从我发布的两个链接中进行了以下设置:

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)我确实获得了我想要的相关图库应用程序,但我无法添加我的活动。

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

此外,当我单击它时,没有打开任何服务。但是,这可能与 '1' arg in 有关,startActivityOnResult因为我不确定该 arg 应该放什么。

4

1 回答 1

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();
于 2016-01-06T01:09:18.347 回答