从图库或Google照片中选择媒体意图的问题

时间:2019-03-06 05:31:50

标签: android google-photos

如何捕捉哪个用户选择以编程方式选择媒体? Google图片或图库

1 个答案:

答案 0 :(得分:0)

查看此代码以从“图库”或“ Google相册”中选择图像或视频

 int PICK_IMAGE_REQUEST = 111;


imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)

                String[] mimeTypes = {"video/*","image/*"};

    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        intent.setType(mimeTypes.length == 1 ? mimeTypes[0] : "*/*");
        if (mimeTypes.length > 0) {
            intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
        }
    } else {
        String mimeTypesStr = "";
        for (String mimeType : mimeTypes) {
            mimeTypesStr += mimeType + "|";
        }
        intent.setType(mimeTypesStr.substring(0,mimeTypesStr.length() - 1));
    }
    startActivityForResult(Intent.createChooser(intent,"ChooseFile"), PICK_IMAGE_REQUEST);


        }
    });