Android-将外部存储中的图像列出到Recycleview中

时间:2018-12-08 22:14:09

标签: android android-imageview recycler-adapter

我正在尝试做一个简单的事情,它将外部存储中的所有图像都放入回收视图并显示。

我的问题是由于某种原因我收到了null,但我不知道为什么。 我成功地将所有图像放入字符串数组列表。 然后在Reycleview上我得到null异常的错误。 不能发现我做错了什么。

日志显示错误来自Recycler。来自方法“ onBindViewHolder”(我也提到了这一行)。 它摇摇欲坠holder.imgView.setImageURI(imageUri);-仍然不明白为什么, 即使我检查了它是否不为空。

我的代码

          private BootstrapButton btnLoadImage;
private RecyclerView rvImages;
private static final int REQUEST_CODE = 1;
private boolean mStoragePermissions;
private ArrayList<String> imageLists;
private ImageViewAdapter adapter;


     @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    btnLoadImage = (BootstrapButton)findViewById(R.id.btnLoadImges);
    rvImages = (RecyclerView)findViewById(R.id.rvPictures);


    btnLoadImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(mStoragePermissions){
                imageLists = getFilePaths();
                Log.d("MainActivity",imageLists.toString());
                if(imageLists != null)
                    setAdapter(imageLists);
            }else{
                verifyStoragePermissions();
            }

        }
    });
}

private void setAdapter(ArrayList<String> imageLists) {
    adapter = new ImageViewAdapter(getApplication(),imageLists);
    rvImages.setAdapter(adapter);
    rvImages.setLayoutManager(new GridLayoutManager(getApplication(),3));
    btnLoadImage.setVisibility(View.INVISIBLE);
}

    public ArrayList<String> getFilePaths()
{


    Uri u = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    String[] projection = {MediaStore.Images.ImageColumns.DATA};
    Cursor c = null;
    SortedSet<String> dirList = new TreeSet<String>();
    ArrayList<String> resultIAV = new ArrayList<String>();

    String[] directories = null;
    if (u != null)
    {
        c = managedQuery(u, projection, null, null, null);
    }

    if ((c != null) && (c.moveToFirst()))
    {
        do
        {
            String tempDir = c.getString(0);
            tempDir = tempDir.substring(0, tempDir.lastIndexOf("/"));
            try{
                dirList.add(tempDir);
            }
            catch(Exception e)
            {
                Log.d("MainActivity",e.getMessage().toString());
            }
        }
        while (c.moveToNext());
        directories = new String[dirList.size()];
        dirList.toArray(directories);

    }

    for(int i=0;i<dirList.size();i++)
    {
        File imageDir = new File(directories[i]);
        File[] imageList = imageDir.listFiles();
        if(imageList == null)
            continue;
        for (File imagePath : imageList) {
            try {

                if(imagePath.isDirectory())
                {
                    imageList = imagePath.listFiles();

                }
                if ( imagePath.getName().contains(".jpg")|| imagePath.getName().contains(".JPG")
                        || imagePath.getName().contains(".jpeg")|| imagePath.getName().contains(".JPEG")
                        || imagePath.getName().contains(".png") || imagePath.getName().contains(".PNG") )
                {
                    String path= imagePath.getAbsolutePath();

                    resultIAV.add(path);
                }
            }
            //  }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    return resultIAV;

}


public class ImageViewAdapter extends RecyclerView.Adapter<ImageViewHolder>{
    private LayoutInflater inflater;
    private Context context;
    private ArrayList<String> data;

    public ImageViewAdapter(Context context, ArrayList<String> data){
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.data = data;
    }

    @NonNull
    @Override
    public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = inflater.inflate(R.layout.image_item,parent,false);
        return new ImageViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull ImageViewHolder holder, int position) {
            String imgSrc = data.get(position);
            Uri imageUri = Uri.parse(imgSrc);

            if(imageUri != null) {
               //**Here i always getting null exception **
                holder.imgView.setImageURI(imageUri);
            }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

}


public class ImageViewHolder extends RecyclerView.ViewHolder{
    private ImageView imgView;

    public ImageViewHolder(View itemView){
        super(itemView);
        imgView = (ImageView)findViewById(R.id.imageView);
    }
   }
  }

1 个答案:

答案 0 :(得分:0)

ImageViewHolder

ImageView imgView;
中的null是什么 您正在使用

imgView = (ImageView)findViewById(R.id.imageView);

但是,发现findViewById是在您的活动中而不是在 RecyclerView ViewHolder 中,而是在您的 image_item.xml 根目录中找到视图布局。

因此,只需在该视图中使用findViewById:

public ImageViewHolder(View itemView){
    super(itemView);
    imgView = (ImageView) itemView.findViewById(R.id.imageView);
}