添加我的解决方案 - 无论是来自相机还是来自意图旁边的厨房,它都会返回一个回调:
public class ImagePickerManager extends BaseAdapter {
private List<ResolveInfo> mApplications;
private TreeSet<Integer> mImageCaptureIntents;
private TreeSet<Integer> mImagePickerIntents;
private Context mContext;
private final ImagePickerManagerListener listener;
private static enum intentType {
choosePhoto,
takePhoto,
unknown;
public int getIntValue() {
switch (this) {
case choosePhoto:
return 0;
case takePhoto:
return 1;
case unknown:
return 2;
}
return 0;
}
}
public interface ImagePickerManagerListener {
void onChooseImage(Intent intent);
void onCaptureImage(Intent intent);
}
public ImagePickerManager(Context context,ImagePickerManagerListener listenr) {
this.mContext = context;
this.listener = listenr;
mImageCaptureIntents = new TreeSet<>();
mImagePickerIntents = new TreeSet<>();
//Picking photo intent
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
mApplications = mContext.getPackageManager().queryIntentActivities(intent, 0);
int index = 0;
for (int i = 0; i < mApplications.size(); i++) {
mImagePickerIntents.add(index);
index++;
}
//Capture photo intent
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
List<ResolveInfo> resolveInfoList = mContext.getPackageManager().queryIntentActivities(intent, 0);
mApplications.addAll(resolveInfoList);
for (int i = 0; i < mApplications.size(); i++) {
mImageCaptureIntents.add(index);
index++;
}
}
public static void openChooseAndCaptureImageDialog(final Context context, final ImagePickerManagerListener listener) {
Log.d("openChooseAndCaptureImageDialog", "enter");
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
final ImagePickerManager imagePickerManager = new ImagePickerManager(context,listener);
builder.setTitle(context.getString(R.string.image_picker_dialog_box_title));
builder.setAdapter(imagePickerManager, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialoginterface, int i) {
ResolveInfo resolveInfo = (ResolveInfo) imagePickerManager.getItem(i);
Intent pickerIntent = imagePickerManager.getIntentForPackage(context,resolveInfo,i);
switch (imagePickerManager.getIntentType(i)){
case choosePhoto:
listener.onChooseImage(pickerIntent);
break;
case takePhoto:
listener.onCaptureImage(pickerIntent);
break;
case unknown:
break;
}
}
});
builder.setCancelable(true);
builder.setInverseBackgroundForced(true);
AlertDialog dialog = builder.create();
dialog.show();
}
private intentType getIntentType(int index) {
if (mImageCaptureIntents.contains(index)) {
return intentType.takePhoto;
} else if(mImagePickerIntents.contains(index)) {
return intentType.choosePhoto;
}
return intentType.unknown;
}
private Intent getIntentForPackage(Context context, ResolveInfo info,int index) {
Intent intent = context.getPackageManager().getLaunchIntentForPackage(info.activityInfo.packageName);
ComponentName chosenName = new ComponentName(
info.activityInfo.packageName,
info.activityInfo.name);
intent.setComponent(chosenName);
intent.setFlags(Intent.FLAG_ACTIVITY_TASK_ON_HOME);
if (mImageCaptureIntents.contains(index)) {
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
} else if(mImagePickerIntents.contains(index)) {
intent.setType("image/*");
intent.setAction(Intent.ACTION_PICK);
}
return intent;
}
@Override
public int getCount() {
return mApplications.size();
}
@Override
public Object getItem(int position) {
return mApplications.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ResolveInfo item = mApplications.get(position);
if (convertView == null) {
TextView applicationTextView = new TextView(mContext);
LayoutParams param = new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
applicationTextView.setLayoutParams(param);
final int horizontalPadding = (int) FVRGeneralUtils.convertDpToPx(mContext, 15);
final int verticalPadding = (int) FVRGeneralUtils.convertDpToPx(mContext, 5);
applicationTextView.setPadding(horizontalPadding,verticalPadding, horizontalPadding, verticalPadding);
applicationTextView.setGravity(android.view.Gravity.CENTER_VERTICAL);
Resources.Theme th = mContext.getTheme();
TypedValue tv = new TypedValue();
if (th.resolveAttribute(android.R.attr.textAppearanceMedium, tv, true)) {
applicationTextView.setTextAppearance(mContext, tv.resourceId);
}
applicationTextView.setMinHeight((int) FVRGeneralUtils.convertDpToPx(mContext, 25));
applicationTextView.setCompoundDrawablePadding((int) FVRGeneralUtils.convertDpToPx(mContext, 7));
convertView = applicationTextView;
}
TextView textView = (TextView) convertView;
textView.setText(item.loadLabel(mContext.getPackageManager()));
textView.setCompoundDrawablesWithIntrinsicBounds(item.loadIcon(mContext.getPackageManager()), null, null, null);
return textView;
} }