queryIntentActivityOptions 不起作用 [英] queryIntentActivityOptions not working
问题描述
我正在尝试创建一个对话框,显示用户手机中的所有应用程序,这些应用程序可用于从存储中选择图片或使用相机拍摄.
这是对
编辑
Listlaunchables=pm.queryIntentActivityOptions(this.getComponentName(),new Intent[]{takePicture},photoPickerIntent,0);
您没有在列表视图中传递正确的适配器
您的代码
Listlaunchables=pm.queryIntentActivityOptions(this.getComponentName(),new Intent[]{takePicture},photoPickerIntent,0);Collections.sort(launchables,新的 ResolveInfo.DisplayNameComparator(pm));appAdapter=new AppAdapter(pm, launchables);lv.setAdapter(适配器);
正确方法
Listlaunchables=pm.queryIntentActivityOptions(this.getComponentName(),new Intent[]{takePicture},photoPickerIntent,0);Collections.sort(launchables,新的 ResolveInfo.DisplayNameComparator(pm));appAdapter=new AppAdapter(pm, launchables);lv.setAdapter(appAdapter);
注意区别
lv.setAdapter(appAdapter);
结果
I 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.
This is a follow-up to my previous question.
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 it's not working. my listview isn't been populated with apps that can be used to access an image or take one using the camera.
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[]{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);
//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));
}
}
RESULT(empty dialog)
EDIT
List<ResolveInfo> launchables=pm.queryIntentActivityOptions(
this.getComponentName(),new Intent[]{takePicture},
photoPickerIntent,0);
You are not passing the correct adapter in your listview
Your code
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);
Correct way
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(appAdapter);
Note the difference in
lv.setAdapter(appAdapter);
Result
这篇关于queryIntentActivityOptions 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!