如何限制用户仅从Android的MediaStore中挑选照片?

low*_*llk 1 video android photo mediastore

我希望用户能够从他们的媒体库中选择一张照片,但不希望他们能够选择一个视频.我无法限制他们只选择照片.当我使用打开图库应用程序时

new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
Run Code Online (Sandbox Code Playgroud)

然后他们可以选择照片和视频.有没有办法限制他们只选择照片?

Vit*_*nko 5

为此,您还需要使用mime类型设置为"image/*" Intent.setType(String type).

更新:看起来像是一次设置Uri和mime类型的正确方法是:

Intent intent = new Intent(Intent.ACTION_PICK);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
Run Code Online (Sandbox Code Playgroud)

更新2:这是因为当我们单独设置mime类型或数据uri时,看看会发生什么(取自Intent源代码):

public Intent setType(String type) {
    mData = null;
    mType = type;
    return this;
}

public Intent setData(Uri data) {
    mData = data;
    mType = null;
    return this;
}
Run Code Online (Sandbox Code Playgroud)

至少在API 2.2上也是如此.