Reputation: 45
I'm making an app that, in theory, have to load images from my internal storage an showing it up on a RecyclerView. I think I followed all the steps correctly and after many unsuccessful tries I'm asking here. So I think the code to catch up the data to the adapter is wrong, but I cannot think of anything to verify that. Any help, Observation, suggestions are greatly appreciated. These are my files:
Photos.java (Where I get the data):
public class Photos {
static ArrayList<String> filenameListTemp=new ArrayList<>();
static ArrayList<File> photosList=new ArrayList<>();
public static ArrayList<String> getPhotos(File internal){
File listFolers[]=internal.listFiles();
if (listFolers!=null && listFolers.length>0){
for (File file:listFolers) {
if (file.isDirectory()){
getPhotos(file);
}
else {
if (file.getName().endsWith(".png")||file.getName().endsWith(".jpg")||file.getName().endsWith(".gif")||file.getName().endsWith(".bmp")){
String temp =file.getPath().substring(0,file.getPath().lastIndexOf('/'));
if (!filenameListTemp.contains(temp)){
filenameListTemp.add(temp); //This add names
photosList.add(file);// This add Files
}
}
}
}
}
return filenameListTemp;
}
}
RecyclerViewAdapter:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
Context context;
ArrayList<String> photos=new ArrayList<>();
LayoutInflater layoutinflater;
public RecyclerViewAdapter(Context context, ArrayList<String> photos) {
this.context=context;
this.photos=photos;
layoutinflater=LayoutInflater.from(context);
}
@Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=layoutinflater.inflate(R.layout.recyclerview_item_list,parent,false);
RecyclerViewAdapter.ViewHolder holder=new RecyclerViewAdapter.ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(RecyclerViewAdapter.ViewHolder holder, int position) {
holder.image.setImageBitmap(BitmapFactory.decodeFile(photos.get(position)));
}
@Override
public int getItemCount() {
return photos.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView image;
public ViewHolder(View itemView) {
super(itemView);
image= (ImageView) itemView.findViewById(R.id.image);
}
}
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerViewAdapter adapter;
File expath= Environment.getExternalStorageDirectory();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView= (RecyclerView) findViewById(R.id.recyclerview);
adapter=new RecyclerViewAdapter(this, Photos.getPhotos(expath));
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
}
}
Upvotes: 2
Views: 350
Reputation: 31
Please make sure that the arraylist you're passing in adapter have size greater then 0. If the size is 0 then it will show anything in adapter
Thanks!!
Upvotes: 0
Reputation: 21736
1. Make sure you have added permission in AndroidManifest.xml
to read external storage.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
2. Put some logs
in getPhotos()
method, to check the size of filenameListTemp
and photosList
to know weather its working or not.
Upvotes: 2