首页 > 解决方案 > getClipData() returning null when a single image is selected when working fine when multiple images are selected

问题描述

基本上,我需要选择多个图像。当我选择多个图像时,我的代码工作正常,但当我选择单个图像时它不起作用。

我正在使用此代码来选择图像

public void getPhotoFromGallery(){
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent,"Select Picture"), 1);
}

我的 onActivityResult() 是

super.onActivityResult(requestCode, resultCode, data);
    Log.i("onActivityResult: ", "STARTED");
    if(requestCode == 1 && resultCode == RESULT_OK && data!=null){
        try {
            Log.i("Error: ", String.valueOf(data));
            ClipData mClipData = data.getClipData();
            Log.i("Error: ", String.valueOf(mClipData));
            .....

            .....
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
}

当我选择多个图像时,我的代码工作正常。但是当我选择单个图像时, data.getClipData() 返回 null。我基本上需要所有选择图像的 URI 列表。我不明白这个问题。另外,string.valueof(数据)显示选择一个图像时存在URI,但是data.getClipdata()仍返回null。

标签: android

解决方案


此外, String.valueof(data) 显示选择一个图像时存在 URI,但 data.getClipData() 仍返回 null

你会Uri从电话中得到,交付到data.getData()哪里。dataUrionActivityResult()


推荐阅读