将图像URI插入GridView



我正在尝试使用GridView显示来自DB的图像,但它无法正常工作,并且没有错误。当我重新使用该行

时,代码还可以
Glide.with(context).load(images.get(position)).into(imageView);

使用线imageView.setImageResource(R.drawable.photo);它的工作很好。URI似乎有问题,但是URI很好,我在其他地方使用相同的URI。

public class PhotoPreviewAdapter extends BaseAdapter {
private ArrayList<String> images;
private Context context;
public PhotoPreviewAdapter(Context context, ArrayList<String> images){
    this.context = context;
    this.images = images;
}
@Override
public int getCount() {
    if(images == null){
        return 0;
    }
    return images.size();
}
@Override
public Object getItem(int position) {
    return images.get(position);
}
@Override
public long getItemId(int position) {
    return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if(convertView == null) {
        convertView = LayoutInflater.from(context).inflate(R.layout.grid_item, parent, false);
    }
    ImageView imageView = (ImageView) convertView.findViewById(R.id.grid_item_image_view);
    //imageView.setImageURI(Uri.fromFile(new File(context.getFilesDir(), images.get(position))));
    //Uri uri = Uri.parse(images.get(position));
    Glide.with(context).load(images.get(position)).into(imageView);
    //imageView.setImageResource(R.drawable.takephoto);
    //Toast.makeText(context, "" + images.get(position).toString(), Toast.LENGTH_SHORT).show();
    return convertView;
}

}

public class Photo_preview extends AppCompatActivity {
private SQLiteDatabase db;
private Cursor c;
private int frontImgCol, sideImgCol, backImgCol;
private GridView gridView;
private PhotoPreviewAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_photo_preview);
    DbHelper dbHelper = new DbHelper(this);
    db = dbHelper.getWritableDatabase();
    gridView = (GridView) findViewById(R.id.gridView);
    c = db.rawQuery("SELECT * FROM " + DbHelper.measuresTable.MEASUREMENTS_TABLE + " ORDER BY " + DbHelper.measuresTable.COL_DATETOSORT + " DESC", null);
    frontImgCol = c.getColumnIndex(DbHelper.measuresTable.COL_FRONTIMG);
    sideImgCol = c.getColumnIndex(DbHelper.measuresTable.COL_SIDEIMAGE);
    backImgCol = c.getColumnIndex(DbHelper.measuresTable.COL_BACKIMG);
    ArrayList<String> images = new ArrayList<>();
    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
        images.add(c.getString(frontImgCol));
        images.add(c.getString(sideImgCol));
        images.add(c.getString(backImgCol));
    }
    adapter = new PhotoPreviewAdapter(this, images);
    gridView.setAdapter(adapter);
}

}

尝试此

Glide.with(context)
     .load(Uri.parse(images.get(position)))
     .into(imageView);

或此

Glide.with(context)
         .load(Uri.fromFile(new File(images.get(position))))
         .into(imageView);

最新更新