在相机和图库之间选择图像选择

jna*_*man 7 android android-intent android-gallery android-camera android-intent-chooser

我试图让用户从图库中选择图像,或者用相机拍照.我试过这个:

        Intent imageIntent = new Intent(Intent.ACTION_GET_CONTENT);
        imageIntent.setType("image/*");
        startActivityForResult(Intent.createChooser(imageIntent, "Select Picture"), GET_IMAGE_REQUEST);
Run Code Online (Sandbox Code Playgroud)

但它自动显示图库,甚至没有提供选择活动的选项.似乎应该有一些更好的方法来实现这个,而不是在这个问题中给出的解决方案.这真的是唯一的方法吗?

Mar*_*sco 13

我已经合并了一些解决方案,以便从Gallery或Camera中选择一个完整的util.这些是ImagePicker util的功能(也在Github lib中):

  • 合并了Gallery和Camera resquests的意图.
  • 调整所选大图像的大小(例如:2500 x 1600)
  • 如果需要,旋转图像

截图:

ImagePicker开始意图

编辑:这是一个代码片段,用于将Gallery和Camera应用程序合并为Intent.你可以在ImagePicker util上看到完整的代码(也在Github lib中)

public static Intent getPickImageIntent(Context context) {
    Intent chooserIntent = null;

    List<Intent> intentList = new ArrayList<>();

    Intent pickIntent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    takePhotoIntent.putExtra("return-data", true);
    takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile(context)));
    intentList = addIntentsToList(context, intentList, pickIntent);
    intentList = addIntentsToList(context, intentList, takePhotoIntent);

    if (intentList.size() > 0) {
        chooserIntent = Intent.createChooser(intentList.remove(intentList.size() - 1),
                context.getString(R.string.pick_image_intent_text));
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentList.toArray(new Parcelable[]{}));
    }

    return chooserIntent;
}

private static List<Intent> addIntentsToList(Context context, List<Intent> list, Intent intent) {
    List<ResolveInfo> resInfo = context.getPackageManager().queryIntentActivities(intent, 0);
    for (ResolveInfo resolveInfo : resInfo) {
        String packageName = resolveInfo.activityInfo.packageName;
        Intent targetedIntent = new Intent(intent);
        targetedIntent.setPackage(packageName);
        list.add(targetedIntent);
    }
    return list;
}
Run Code Online (Sandbox Code Playgroud)


Kri*_*adi 7

你应该在你的应用程序中执行此逻辑.从画廊中挑选图像并使用相机拍照正在使用不同的意图.

我建议您使用按钮(或任何用户界面选择操作的UI)并为两个操作创建两个单独的方法.比方说,你创建了两个名为btnPickGallery和的按钮btnTakePicture.

比如说onBtnPickGallery,两个按钮都会触发自己的动作onBtnTakePicture.

public void onBtnPickGallery() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), GALLERY_REQUEST_CODE);
}

public void onBtnTakePicture() {
    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File photo = new File(Environment.getExternalStorageDirectory(), "dir/pic.jpg");

    Uri outputFileUri = Uri.fromFile(photo);

    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
    startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);
}
Run Code Online (Sandbox Code Playgroud)

然后你可以使用onActivityResult()方法获取结果.