我是应用程序开发的新手,我一直在尝试创建一个图库应用程序。但是当我点击打开应用程序时,没有显示任何内容。我希望它能够在Gridview中显示存储在SD卡上的图像。我已经阅读了很多关于SO的问题,但是它们并没有太多帮助。我的代码中找不到任何错误。任何人都可以告诉我代码有什么问题吗?
imageGallery.java :
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.GridView;
import java.io.File;
import java.util.ArrayList;
public class imageGallery extends Activity {
ArrayList<String> images;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_gallery);
images = new ArrayList<>();// list of file paths
getFromSdcard();
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this, images));
}
File[] listFile;
public void getFromSdcard() {
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "InsCam + FixBlur");
if (listFile != null && listFile.length > 0) {
for (File file1 : listFile) {
if (file1.isDirectory()) {
listFile = file.listFiles();
} 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 = file1.getPath().substring(0, file1.getPath().lastIndexOf('/'));
if (!images.contains(temp))
images.add(temp);
}
}
}
}
}
}
ImageAdapter.java :
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import java.util.List;
public class ImageAdapter extends BaseAdapter {
private static final int PADDING = 8;
private static final int WIDTH = 250;
private static final int HEIGHT = 250;
private Context mContext;
private List<String> mThumbIds;
public ImageAdapter(Context c, List<String> ids){
mContext = c;
this.mThumbIds = ids;
}
@Override
public int getCount() {
return mThumbIds.size();
}
@Override
public Object getItem(int position) {
return null;
}
// Will get called to provide the ID that
// is passed to OnItemClickListener.onItemClick()
@Override
public long getItemId(int position) {
return position;
}
// create a new ImageView for each item referenced by the Adapter
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = (ImageView) convertView;
// if convertView's not recycled, initialize some attributes
if (imageView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(WIDTH, HEIGHT));
imageView.setPadding(PADDING, PADDING, PADDING, PADDING);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
}
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(mThumbIds.get(position), options);
// Set inSampleSize
options.inSampleSize = 4;
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
Bitmap myBitmap = BitmapFactory.decodeFile(mThumbIds.get(position), options);
imageView.setImageBitmap(myBitmap);
return imageView;
}
}
答案 0 :(得分:0)
适配器中的getItem方法返回null似乎是个问题。在转换字符串路径之后,您应该返回字符串路径或位图,因为返回类型是Object。所以我建议这样做。
class ImageAdapter extends BaseAdapter{
@Override
public Object getItem(int position) {
//I have shown string example here.. you can update as per your needs..
return mThumbIds.get(position);
}
}
希望这会有所帮助..