我想从画廊创建一个图片选择器。我使用代码

 intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
 startActivityForResult(intent, TFRequestCodes.GALLERY);

我的问题是在这个活动和视频文件显示。是否有一种方法可以过滤显示的文件,以便在此活动中不显示视频文件?


当前回答

有时,您无法从所选择的图片中获取文件。 这是因为选择一个来自谷歌+,Drive, Dropbox或任何其他提供商。

最好的解决方案是让系统通过Intent来选择内容。ACTION_GET_CONTENT并使用内容提供程序获取结果。

您可以按照下面的代码或查看我更新的要点。

public void pickImage() {
  Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
  intent.setType("image/*");
  startActivityForResult(intent, PICK_PHOTO_FOR_AVATAR);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == PICK_PHOTO_FOR_AVATAR && resultCode == Activity.RESULT_OK) {
        if (data == null) {
            //Display an error
            return;
        }
        InputStream inputStream = context.getContentResolver().openInputStream(data.getData());
        //Now you can do whatever you want with your inpustream, save it as file, upload to a server, decode a bitmap...
    }
}

其他回答

由于startActivityForResult()已经废弃,我们可以使用ActivityResultLauncher以以下方式从图库中选择图像:

首先,我们需要定义一个ActivityResultLauncher<String[]>并在onCreate()(用于活动)或onViewCreated()(用于片段)中初始化它

        ActivityResultLauncher<String[]> galleryActivityLauncher = registerForActivityResult(new ActivityResultContracts.OpenDocument(), new ActivityResultCallback<Uri>() {
            @Override
            public void onActivityResult(Uri result) {
                if (result != null) {
                    // perform desired operations using the result Uri
                } else {
                    Log.d(TAG, "onActivityResult: the result is null for some reason");
                }
            }
        });

假设我们需要在点击submitButton时打开图库。

在onClickListener中,我们需要调用

galleryActivityLauncher.launch(new String[]{"image/*"});

这里的技巧是launch()的参数。通过在参数数组中添加"image/*",我们指定文件资源管理器只加载图像。

2021 Kotlin解决方案与新版本的Fragment:

dependencies {
  implementation "androidx.fragment:fragment:1.3.3"
}
class YourFragment : Fragment() {

    private val fileChooserContract = registerForActivityResult(ActivityResultContracts.GetContent()) { imageUri ->
        if (imageUri != null) {
            // imageUri now contains URI to selected image
        }
    }

    // ...

    fun openFileChooser() {
        fileChooserContract.launch("image/*")
    }
}

你可以做的比下面的回答更简单:

Uri Selected_Image_Uri = data.getData();
ImageView imageView = (ImageView) findViewById(R.id.loadedimg);
imageView.setImageURI(Selected_Image_Uri);

对于Kotlin使用新的ActivityResultContracts,因为startActivityForResult是废弃的:

private val mSelectedPicDataResult = registerForActivityResult(
    ActivityResultContracts.StartActivityForResult()
) { result ->
    if (result.resultCode == Activity.RESULT_OK) {
        val selectedPicUri = result.data?.data
        //use your selected pic
    }
}

private fun startSelectPic() {
    val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
    intent.addCategory(Intent.CATEGORY_OPENABLE)
    intent.type = "image/*"
    mSelectedPicDataResult.launch(intent)
}

对于只从本地选择添加这个:

        i.putExtra(Intent.EXTRA_LOCAL_ONLY,true)

这个工作得很好:

    val i = Intent(Intent.ACTION_GET_CONTENT,MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
    i.type = "image/*"
    i.putExtra(Intent.EXTRA_LOCAL_ONLY,true)
    startActivityForResult(Intent.createChooser(i,"Select Photo"),pickImageRequestCode)