2

I have a list which contain apps that can handle any of my two actions Intent.ACTION_PICK and MediaStore.ACTION_IMAGE_CAPTURE. i later on use an array adapter to populate my listivew with the information from this list.

In my onItemClickListener, am able to acquire ActivityInfo and ComponentName from ResolveInfo instance.but am not able to get IntentFilter instance from ResolveInfo (attempt to get it throws NullPointerException), AND YET any application that is in my list MUST HAVE resolved one of my intents for it to be ln the list

This is the line throwing nullpointerexception IntentFilter filter = launchable.filter;

private void acquirePicture(){

ListView lv=(ListView)dialog.findViewById(R.id.listView1);

PackageManager pm=getPackageManager();

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

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);

IntentFilter filter = launchable.filter;
            if(filter.actionsIterator() != null){
                Iterator<String > actions = filter.actionsIterator();
            }

            int actioncode;
            Intent  intent = new Intent();
            Uri uri;
            if(filter.hasAction(Intent.ACTION_PICK)){
                actioncode = 1;
                uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                intent.setData(uri);
            }else{
                actioncode = 0;
            }
            intent.setComponent(name);
            startActivityForResult(intent,actioncode);

    }
});

}

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));
}
}

Question

How can i get the intent(s) which the selected app resolved in my onClickListner body?

David Wasser
  • 93,459
  • 16
  • 209
  • 274
Edijae Crusar
  • 3,473
  • 3
  • 37
  • 74

1 Answers1

1

You need to explicitly say that you want the filter returned. Instead of:

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

Use this:

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

I haven't actually tested this. Please be aware that there are a lot of bugs and missing functionality related to returning IntentFilters. In a lot of cases the documentation is wrong, in a lot of cases the code just doesn't do what you would expect it to do, and there aren't a lot of good working examples.

Try this and let us know if it works.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • i don't really understand the 2nd and 3rd parameters of `queryIntentActivityOptions()` what am doing is guess work. i din't understand them well from android documentation since there wasn't even an example. do you understand this method well? – Edijae Crusar Dec 16 '15 at 05:41
  • for the answer, it's perfect. worked well. i highly appreciate your help. i din't understand well the 3rd parameter in `queryIntentActivityOptions()` **that's why i was putting a zero**. please give me feedback on my previous comment(one above). – Edijae Crusar Dec 16 '15 at 06:00
  • 1
    Some fields of the `ResolveInfo` are not filled in by default; the `GET_` constants must be used to request them. – j__m Sep 25 '16 at 16:31
  • @gikarasojokinene For some reason I didn't see your comments. Sorry about that. Basically @j__m provided the answer. The third argument is a set of flags. You need to specify, using these flags, which information you want returned in the `ResolveInfo` objects, as by default a lot of information is not returned. – David Wasser Sep 25 '16 at 17:47