如何使用queryIntentActivityOptions()方法 [英] how to use queryIntentActivityOptions() method

查看:388
本文介绍了如何使用queryIntentActivityOptions()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想创建一个对话框,其中显示在可用于选择从所述存储的图像,或使用相机拍摄之一的用户电话的所有应用程序。下面是我的两个意图是我计划使用。

 意图photoPickerIntent =新意图(Intent.ACTION_PICK);
    photoPickerIntent.setType(图像/ *);意图takePicture =新意图(MediaStore.ACTION_IM​​AGE_CAPTURE);

我发现来填充我的列表视图中我的,可以执行上述操作的应用程序定制对话框的最好方法是使用 queryIntentActivityOptions()方法,但我被卡住。


  • 我以前从未使用过它,我不知道我是否做了正确的方式


  • 我不知道我将填充的ListView我从 queryIntentActivityOptions()方法。

  • 所获取的应用
  • 如何实施 onItemClickListener 在我的列表视图这是我的自定义对话框。


这是我的方法包含对话框。 请我研究,但还没有找到一个很好的教程,指导我如何,我可以实现 queryIntentActivityOptions()

 私人无效acquirePicture(){
    意图photoPickerIntent =新意图(Intent.ACTION_PICK);
    photoPickerIntent.setType(图像/ *);    意图takePicture =新意图(MediaStore.ACTION_IM​​AGE_CAPTURE);
    startActivityForResult(photoPickerIntent,1);    最后对话的对话=新的对话框(本);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    WindowManager.LayoutParams WMLP = dialog.getWindow()的getAttributes()。
    WMLP.gravity = Gravity.CENTER;
    。dialog.getWindow()setAttributes(WMLP);
    dialog.getWindow()。setBackgroundDrawable(
            新ColorDrawable(android.graphics.Color.TRANSPARENT));
    dialog.setCanceledOnTouchOutside(真);
    dialog.setContentView(R.layout.about_dialog);
    dialog.setCancelable(真);
    LV的ListView =(ListView控件)dialog.findViewById(R.id.listView1);
    软件包管理系统下午= getPackageManager();    清单< ResolveInfo> launchables = pm.queryIntentActivityOptions(
this.getComponentName(),新的意向[] {photoPickerIntent,takePicture},
空,PackageManager.MATCH_DEFAULT_ONLY);    Collections.sort(launchables,
            新ResolveInfo.DisplayNameComparator(PM));    appAdapter =新AppAdapter(时许,launchables);    lv.setAdapter(适配器);
    lv.setOnItemClickListener(新AdapterView.OnItemClickListener(){
        @覆盖
        公共无效onItemClick(适配器视图<>为arg0,ARG1查看,INT位置,
                                长ARG3){
            // TODO自动生成方法存根
            ResolveInfo可启动= appAdapter.getItem(位置);
            ActivityInfo活性= launchable.activityInfo;
            组件名NAME =新的组件名(activity.applicationInfo.packageName,
                    activity.name);
            //我不知道下一步做什么,也无论是否在做IT
             正确的方法
        }
    });    dialog.show();
}类AppAdapter扩展ArrayAdapter< ResolveInfo> {
    私人软件包管理系统PM = NULL;    AppAdapter(软件包管理系统时,列表< ResolveInfo>应用){
        超(Custom_chooser.this,R.layout.row,应用);
        this.pm =时;
    }    @覆盖
    公共查看getView(INT位置,查看convertView,
                        父母的ViewGroup){
        如果(convertView == NULL){
            convertView = NewView的(父);
        }        bindView(位置,convertView);        回报(convertView);
    }    私人查看NewView的(父的ViewGroup){
        返回(getLayoutInflater()膨胀(R.layout.row,父母,FALSE));
    }    私人无效bindView(INT位置,查看行){
        TextView的标签=(TextView中)row.findViewById(R.id.label);        label.setText(的getItem(位置).loadLabel(PM));        ImageView的图标=(ImageView的)row.findViewById(R.id.icon);        icon.setImageDrawable(的getItem(位置).loadIcon(PM));
    }
}


解决方案

检查工作之下code打开,相机通过画廊在一次浏览。

  //首先精选相机。
最终名单<&意向GT; cameraIntents =新的ArrayList<&意向GT;();
最终意图captureIntent =新意图(
        android.provider.MediaStore.ACTION_IM​​AGE_CAPTURE);
最终的软件包管理系统软件包管理系统= getPackageManager();
最终名单< ResolveInfo> listCam = packageManager.queryIntentActivities(
        captureIntent,0);
对于(ResolveInfo RES:listCam){
  最后弦乐的packageName = res.activityInfo.packageName;
  最终意向意图=新意图(captureIntent);
  intent.setComponent(新组件名(res.activityInfo.packageName,
          res.activityInfo.name));
  intent.setPackage(的packageName);
  intent.putExtra(MediaStore.EXTRA_OUTPUT,outputFileUri);
  cameraIntents.add(意向);
}最终意图galleryIntent =新意图();
galleryIntent.setType(图像/ *);
galleryIntent.setAction(Intent.ACTION_PICK);//文件系统的选项选择器。
最终意图chooserIntent = Intent.createChooser(galleryIntent,
        选择图片);//添加摄像头的选项。
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
        cameraIntents.toArray(新Parcelable [] {}));startActivityForResult(chooserIntent,TAKE_PHOTO_ code);

这会帮助你。!!

引用链接和教程<一个href=\"http://stackoverflow.com/questions/34286447/queryintentactivityoptions-not-working/34287834#34287834\">click这里。

Am trying to create a dialog that shows all applications in a user phone that can be used to select a picture from the storage or take one using the camera. below are my two intents that am planning to use.

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
    photoPickerIntent.setType("image/*");

Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

The best way i have found to populate my listview in my customized dialog with applications that can perform the above actions is to use queryIntentActivityOptions() method but am stucked.

  • i have never used it before and i don't know whether am doing it the right way

  • i don't know how i will populate my listview with the gotten applications from queryIntentActivityOptions() method.

  • How to implement onItemClickListener in my listview which is in my customized dialog.

This is my method that contain the dialog. please i have researched but haven't found a good tutorial to guide me on how i can implement queryIntentActivityOptions()

private void acquirePicture(){
    Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
    photoPickerIntent.setType("image/*");

    Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(photoPickerIntent, 1);

    final Dialog dialog = new Dialog(this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    WindowManager.LayoutParams WMLP = dialog.getWindow().getAttributes();
    WMLP.gravity = Gravity.CENTER;
    dialog.getWindow().setAttributes(WMLP);
    dialog.getWindow().setBackgroundDrawable(
            new ColorDrawable(android.graphics.Color.TRANSPARENT));
    dialog.setCanceledOnTouchOutside(true);
    dialog.setContentView(R.layout.about_dialog);
    dialog.setCancelable(true);
    ListView lv=(ListView)dialog.findViewById(R.id.listView1);
    PackageManager pm=getPackageManager();

    List<ResolveInfo> launchables=pm.queryIntentActivityOptions(
this.getComponentName(),new Intent[]{photoPickerIntent,takePicture},
null,PackageManager.MATCH_DEFAULT_ONLY);

    Collections.sort(launchables,
            new ResolveInfo.DisplayNameComparator(pm));

    appAdapter=new AppAdapter(pm, launchables);

    lv.setAdapter(adapter);
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                                long arg3) {
            // TODO Auto-generated method stub
            ResolveInfo launchable=appAdapter.getItem(position);
            ActivityInfo activity=launchable.activityInfo;
            ComponentName name=new ComponentName(activity.applicationInfo.packageName,
                    activity.name);
            //I DON'T KNOW WHAT TO DO NEXT OR WHETHER AM DOING IT
             THE CORRECT WAY
        }
    });

    dialog.show();
}

class AppAdapter extends ArrayAdapter<ResolveInfo> {
    private PackageManager pm=null;

    AppAdapter(PackageManager pm, List<ResolveInfo> apps) {
        super(Custom_chooser.this, R.layout.row, apps);
        this.pm=pm;
    }

    @Override
    public View getView(int position, View convertView,
                        ViewGroup parent) {
        if (convertView==null) {
            convertView=newView(parent);
        }

        bindView(position, convertView);

        return(convertView);
    }

    private View newView(ViewGroup parent) {
        return(getLayoutInflater().inflate(R.layout.row, parent, false));
    }

    private void bindView(int position, View row) {
        TextView label=(TextView)row.findViewById(R.id.label);

        label.setText(getItem(position).loadLabel(pm));

        ImageView icon=(ImageView)row.findViewById(R.id.icon);

        icon.setImageDrawable(getItem(position).loadIcon(pm));
    }
}

解决方案

check the below working code to open, Camera and browse through gallery at once.

 // Picks Camera first.
final List<Intent> cameraIntents = new ArrayList<Intent>();
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, outputFileUri);
  cameraIntents.add(intent);
}

final Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_PICK);

// Chooser of filesystem options.
final Intent chooserIntent = Intent.createChooser(galleryIntent,
        "Select Image from");

// Add the camera options.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
        cameraIntents.toArray(new Parcelable[]{}));

startActivityForResult(chooserIntent, TAKE_PHOTO_CODE);

This will help you.!!

Reference links and tutorials click here.

这篇关于如何使用queryIntentActivityOptions()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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