我想在我的数据库中插入图像,但图像不会插入数据库中,但文本会插入数据库中。我在JSON中从服务器获取数据但是添加了文本,但是当加载JSON时,图像没有添加到我想要的数据库中,然后imageurl存储在数据库中,并且在离线后我访问
我的代码是:
数据库
public class DataBasehelper extends SQLiteOpenHelper {
private static final int DB_VERSION = 1;
private static final String DB_NAME = "MyApp";
private static final String TABLE_TEXTS= "texts";
private static final String TABLE_IMAGES= "images";
private static final String TABLE_SPINNER = "spinners";
private static final String KEY_ID = "_id";
private static final String KEY_NAME = "_name";
private static final String KEY_SPIN = "_spin";
private static final String KEY_URL = "url";
private static final String CREATE_TABLE_IMAGE = "CREATE TABLE "
+ TABLE_IMAGES
+ " ( "
+ KEY_ID + " INTEGER PRIMARY KEY, "
+ KEY_NAME + " TEXT, "
+ KEY_URL + " TEXT) ";
public DataBasehelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_IMAGE);
Log.e("table name","==>"+CREATE_TABLE_TEXTS);
Log.e("table name","==>"+CREATE_TABLE_IMAGE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_IMAGES);
onCreate(db);
}
public void addImage(ImgModel model) {
SQLiteDatabase db = this.getWritableDatabase();
try{
ContentValues values = new ContentValues();
values.put(KEY_NAME, model.getName());
values.put(KEY_URL, model.getImageUrl());
db.insert(TABLE_TEXTS, null, values);
Log.e("this db","value is"+values);
db.close();
}catch (Exception e){
Log.e("problem",e+"");
}
}
public List<ImgModel> getAllImage() {
SQLiteDatabase db = this.getReadableDatabase();
List<ImgModel> imageList = null;
try{
imageList = new ArrayList<ImgModel>();
String QUERY = "SELECT * FROM "+TABLE_TEXTS;
Cursor cursor = db.rawQuery(QUERY, null);
if(!cursor.isLast())
{
while (cursor.moveToNext())
{
ImgModel image = new ImgModel();
image.set_id(cursor.getInt(0));
image.setName(cursor.getString(1));
image.setImageUrl(cursor.getString(2));
imageList.add(image);
}
}
db.close();
}catch (Exception e){
Log.e("error",e+"");
}
return imageList;
}
模型类
public class ImgModel {
private int _id;
private String imageUrl;
private String name;
//Getters and Setters
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int get_id() {
return _id;
}
public void set_id(int _id) {
this._id = _id;
}
}
活动
检查互联网
NetworkUtils utils = new NetworkUtils(getApplicationContext());
if(utils.isConnectingToInternet())
{
getSpinnerData();
}
else
{
List<ImgModel> imageList = db.getAllImage();
adapter = new ImageAdapter(imageList, this);
recyclerView.setAdapter(adapter);
}
在db
中添加图像 private void parseData(JSONArray array) {
for (int i = 0; i < array.length(); i++) {
ImgModel model1 = new ImgModel();
JSONObject json = null;
try {
json = array.getJSONObject(i);
String link = json.getString(Config.TAG_IMAGE_URL);
String imgname = json.getString(Config.TAG_NAME);
model1.setImageUrl(link);
model1.setName(imgname);
db.addImage(model1); // add data in database
Log.e("urls","===>"+link);
} catch (JSONException e) {
e.printStackTrace();
}
listimg.add(model1);
}
答案 0 :(得分:0)
使用像Picasso,Glide或Fresco这样的图像库,它会为你缓存图像,你只需要存储你现在正在做的网址,它只会下载图像一次,之后它会获取图像来自缓存