将图像uri插入gridview

时间:2017-09-22 22:00:02

标签: java android baseadapter android-gridview

我正在尝试使用gridview显示来自db的图像,但它只是不起作用,并且没有错误。 当我重新开始行

时,代码是好的
Glide.with(context).load(images.get(position)).into(imageView);

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

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);
}

}

1 个答案:

答案 0 :(得分:0)

试试这个

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

或者这个

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