将图像插入到gridview中
问题描述:
我试图用gridview显示来自db的图像,但它只是不起作用,并且没有错误。 的代码是好的,当我reaplcing行将图像插入到gridview中
Glide.with(context).load(images.get(position)).into(imageView);
与线imageView.setImageResource(R.drawable.photo);
其做工精细的Cuz。 这似乎有问题与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);
}
}
答
试试这个
Glide.with(context)
.load(Uri.parse(images.get(position)))
.into(imageView);
或者这
Glide.with(context)
.load(Uri.fromFile(new File(images.get(position))))
.into(imageView);
+0
做最后的工作,你可以在评论中看到代码im艾蕾迪试试这个 –
你能共享一个前充足的uri,这是行不通的,可能有问题的格式uri –
文件:///storage/emulated/0/1506112776661.jpg 100%好吧,因为如果我试图显示相同的uri一个imageView的工作。 –