I have been trying to create an imageGallery app to display images in gridview. I want it to display only image directories. But when I open it, images are not displayed. Nothing seems wrong with code. I dont know why its not working.I am not getting any errors or any runtime exceptions. Can anyone help and tell me what am I doing wrong ? Any kind of help is appreciated. Thanks .
imageGallery.java:
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class imageGallery extends Activity {
private ArrayList<String> fileList = new ArrayList<>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_gallery);
GridView imagegrid = (GridView) findViewById(R.id.gridview);
imagegrid.setAdapter(new MyGridAdapter());
File root = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
getFile(root);
}
public ArrayList<String> getFile(File dir) {
File listFile[] = dir.listFiles();
if (listFile != null && listFile.length > 0) {
for (File file : listFile) {
if (file.isDirectory()) {
getFile(file);
} else {
if (file.getName().endsWith(".png")
|| file.getName().endsWith(".jpg")
|| file.getName().endsWith(".jpeg")
|| file.getName().endsWith(".gif")
|| file.getName().endsWith(".bmp")
|| file.getName().endsWith(".webp")) {
String temp = file.getPath().substring(0, file.getPath().lastIndexOf('/'));
if (!fileList.contains(temp))
fileList.add(temp);
}
}
}
}
return fileList;
}
public class MyGridAdapter extends BaseAdapter {
LayoutInflater inflater;
List<GridViewItem> items;
public MyGridAdapter() {
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return 0;
}
@Override
public Object getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.grid_item, null);
}
TextView text = (TextView) convertView.findViewById(R.id.textView);
text.setText(items.get(position).getPath());
ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView);
Bitmap image = items.get(position).getImage();
if (image != null){
imageView.setImageBitmap(image);
}
else {
// If no image is provided, display a folder icon.
imageView.setImageResource(R.drawable.adele1);
}
return convertView;
}
}
}