有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java打算在Android中的相机或图库中进行选择

我正试图启动一个从相机或安卓 gallery中挑选图像的意图。我查看了THIS帖子,目前我的代码即将生效:

private Intent getPickIntent() {
    final List<Intent> intents = new ArrayList<Intent>();
    if (allowCamera) {
        setCameraIntents(intents, cameraOutputUri);
    }
    if (allowGallery) {
        intents.add(new Intent(Intent.ACTION_PICK, 安卓.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI));
    }

    if (intents.isEmpty()) return null;
    Intent result = Intent.createChooser(intents.remove(0), null);
    if (!intents.isEmpty()) {
        result.putExtra(Intent.EXTRA_INITIAL_INTENTS, intents.toArray(new Parcelable[] {}));
    }
    return result;
}

private void setCameraIntents(List<Intent> cameraIntents, Uri output) {
    final Intent captureIntent = new Intent(安卓.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    final PackageManager packageManager = context.getPackageManager();
    final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
    for (ResolveInfo res : listCam) {
        final String packageName = res.activityInfo.packageName;
        final Intent intent = new Intent(captureIntent);
        intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
        intent.setPackage(packageName);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, output);
        cameraIntents.add(intent);
    }
}

当我设置allowCamera=true时,它工作正常

当我设置allowGallery=true时,它会显示以下选择器:

enter image description here

但是如果我设置allowCamera=trueallowGallery =true,则显示的选择器是:

enter image description here

如果选择Android System,则会显示第一个选择器

我希望选择器是这样的:

enter image description here

如何“扩展”Android System选项


共 (2) 个答案

  1. # 1 楼答案

    您可能需要为此创建一个自定义对话框:

    以下是当用户单击特定按钮时要调用的方法的代码:

    private void ChooseGallerOrCamera() {
        final CharSequence[] items = { "Take Photo", "Choose from Gallery",
                "Cancel" };
    
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("Add Photo!");
        builder.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int item) {
                if (items[item].equals("Take Photo")) {
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    File f = new File(android.os.Environment
                            .getExternalStorageDirectory(), "MyImage.jpg");
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
                    startActivityForResult(intent, REQUEST_CAMERA);
                } else if (items[item].equals("Choose from Gallery")) {
                    Intent intent = new Intent(
                            Intent.ACTION_PICK,
                            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    intent.setType("image/*");
                    startActivityForResult(
                            Intent.createChooser(intent, "Select File"),
                            SELECT_FILE);
                } else if (items[item].equals("Cancel")) {
                    dialog.dismiss();
                }
            }
        });
        builder.show();
    }
    

    以及处理onActivityResult()的代码:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            Bitmap mBitmap;
            if (requestCode == REQUEST_CAMERA) {
                File camFile = new File(Environment.getExternalStorageDirectory()
                        .toString());
                for (File temp : camFile.listFiles()) {
                    if (temp.getName().equals("MyImage.jpg")) {
                        camFile = temp;
                        break;
                    }
                }
                try {
    
                    BitmapFactory.Options btmapOptions = new BitmapFactory.Options();
    
                    mBitmap = BitmapFactory.decodeFile(camFile.getAbsolutePath(),
                            btmapOptions);
                    //Here you have the bitmap of the image from camera
    
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else if (requestCode == SELECT_FILE) {
                Uri selectedImageUri = data.getData();
    
                String tempPath = getPath(selectedImageUri, MyActivity.this);
                BitmapFactory.Options btmapOptions = new BitmapFactory.Options();
                mBitmap = BitmapFactory.decodeFile(tempPath, btmapOptions);
    
                //Here you have the bitmap of the image from gallery
            }
        }
    }
    
    public String getPath(Uri uri, Activity activity) {
        String[] projection = { MediaColumns.DATA };
        Cursor cursor = activity
                .managedQuery(uri, projection, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
    

    编辑: 尝试使用以下方法:

    private void letUserTakeUserTakePicture() {
    
        Intent pickIntent = new Intent();
        pickIntent.setType("image/*");
        pickIntent.setAction(Intent.ACTION_GET_CONTENT);
        Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    
        takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                Uri.fromFile(getTempFile(getActivity())));
    
        String pickTitle = "Select or take a new Picture"; // Or get from strings.xml
        Intent chooserIntent = Intent.createChooser(pickIntent, pickTitle);
        chooserIntent.putExtra
                (Intent.EXTRA_INITIAL_INTENTS, new Intent[]{takePhotoIntent});
    
        startActivityForResult(chooserIntent, Values.REQ_CODE_TAKEPICTURE);
    }
    
  2. # 2 楼答案

    In your linked post你可以找到解决方案。代码的不同之处在于画廊意图的创建方式:

    final Intent galleryIntent = new Intent();
    galleryIntent.setType("image/*");
    galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
    

    不是因为ACTION_PICK你是怎么做的,而是因为ACTION_GET_CONTENT。看起来,如果列表中有一个ACTION_PICK(“容器意图”),系统会遍历它以显示拾取的内容,但一旦包含相机意图,它就不能再遍历了(因为有一个直接意图和一个容器意图)

    this answer的注释中,你可以找到ACTION_PICKACTION_GET_CONTENT之间的区别

    有一些解决方案建议使用自定义对话框。但是这个对话框没有标准图标(参见develper docshere)。因此,我建议您继续使用您的解决方案,解决层次结构问题