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 int
First, you need a good understanding of queryIntentActivityOptions()
method of class PackageManager.
QueryIntentActivityOptions(ComponentName caller, Intent[] specifics,Intent intent, int flags)
caller - This is the name of the class you are currently calling this method from. usually we use this.getComponentName
as the value for this parameter.
specifics - This is an array of intents that you want to be first resolved. for your case, you can put any of the two intents you have. i.e photoPickerIntent
or takePictureIntent
like this
new Intent[] {takePicture}
NOTE: this parameter value can be null if you have only one intent. in that case, i think it would be better to use QueryIntentActivities
. but for your case, since you have more than one intent, continue using this method.
intent - Here you place your desired intent. i.e between the two intents that you have, the one you desided not to put in the array because you so it as the most important one
NOTE: this parameter value cannot be null. putting a null value will throw a NullPointerException!
flags - This is like a filter or extra that you want to be included in any of the resolved activites returned by this method.
PackageManager.MATCH_DEFAULT_ONLY - puting this option will filter the resolved activities and select the activities that belong to default applications in your device.e.g if ACTION_IMAGE_CAPTURE
can be resolved by two apps, one which is the default and the other is third party one, e.g you downloaded it, one the default will be selected
PackageManager.GET_INTENT_FILTERS - this option return information about all the intent filters that the resolved app can handle. e.g you can have an app that resolved ACTION_IMAGE_CAPTURE
and yet it can also resolve ACTION_PICK
PackageManager.GET_RESOLVED_FILTER - this option return the intent filter that each of the apps in the list resolved
A WORKING EXAMPLE
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
photoPickerIntent.setType("image/*");
PackageManager pm=getPackageManager();
List launchables=pm.queryIntentActivityOptions(
this.getComponentName(),new Intent[]{takePicture},
photoPickerIntent,PackageManager.GET_RESOLVED_FILTER);
Collections.sort(launchables,
new ResolveInfo.DisplayNameComparator(pm));
You can then populate your listview using the above list of lauchaubles
ListView lv=(ListView)dialog.findViewById(R.id.listView1);
AppAdapter 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 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 {
private PackageManager pm=null;
AppAdapter(PackageManager pm, List 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));
}
}