queryIntentActivityOptions不工作 [英] queryIntentActivityOptions not working
问题描述
我试图创建一个对话框,其中显示了可用于从存储选择的图像或使用相机采取一在用户手机中的所有应用程序。
这是一个后续我的previous问题。
我发现来填充我的列表视图中我的,可以执行上述操作的应用程序定制对话框的最好方法是使用 queryIntentActivityOptions()
方法,但它不工作。 我的ListView不填充了可用于访问图像或使用相机需要一个应用
私人无效acquirePicture(){
意图photoPickerIntent =新意图(Intent.ACTION_PICK);
photoPickerIntent.setType(图像/ *); 意图takePicture =新意图(MediaStore.ACTION_IMAGE_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(),新的意向[] {} takePicture,
photoPickerIntent,0); 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));
}
}
RESULT(空对话)
修改
列表< ResolveInfo> launchables = pm.queryIntentActivityOptions(
this.getComponentName(),新的意向[] {} takePicture,
photoPickerIntent,0);
您没有传递正确的适配器在列表视图
您code
列表< ResolveInfo> launchables = pm.queryIntentActivityOptions(
this.getComponentName(),新的意向[] {} takePicture,
photoPickerIntent,0);Collections.sort(launchables,
新ResolveInfo.DisplayNameComparator(PM));appAdapter =新AppAdapter(时许,launchables);lv.setAdapter(适配器);
的正确方法
列表< ResolveInfo> launchables = pm.queryIntentActivityOptions(
this.getComponentName(),新的意向[] {} takePicture,
photoPickerIntent,0);Collections.sort(launchables,
新ResolveInfo.DisplayNameComparator(PM));appAdapter =新AppAdapter(时许,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屋!