gpt4 book ai didi

android - 从 urls 将图像存储在 Sqlite 中

转载 作者:行者123 更新时间:2023-11-29 03:32:00 26 4
gpt4 key购买 nike

我正在尝试将数据插入 sqlite 数据库,它们都是从 mysql 数据库中获取的名称、消息和图像。问题是如何使用 Blob 处理图像。图像是从 url 获取的。我想把它放在 sqite 表中....我知道他们说最好将它保存在 sdcard 上并将位置放在 sqlite 表中....但唯一的问题是用户可以删除一些我不想要的图像或重命名它们。

这是我的 sqlite 数据库类

public class DBTool extends SQLiteOpenHelper {
private Context appContext;
static final String firstname="firstname";
static final String lastname="lastname";

public DBTool(Context applicationContext){

super(applicationContext, "friend.db", null, 1);
this.appContext = applicationContext;

}
@Override
public void onCreate(SQLiteDatabase database) {

String query = "CREATE TABLE friends ( contactId INTEGER PRIMARY KEY, name TEXT, " +
"message TEXT, image BLOB)";
database.execSQL(query);
}

@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {

String query = "DROP TABLE IF EXISTS friends";

database.execSQL(query);
onCreate(database);

}

public void insertContact(HashMap<String, String> queryValues){

SQLiteDatabase database = this.getWritableDatabase();


ContentValues values = new ContentValues();

values.put("name", queryValues.get("name"));
values.put("message", queryValues.get("message"));
values.put("image", queryValues.get("image"));

database.insert("friends", null, values);
database.close();

}

public int updateContact(HashMap<String, String> queryValues){

SQLiteDatabase database = this.getWritableDatabase();

ContentValues values = new ContentValues();

values.put("name", queryValues.get("name"));
values.put("message", queryValues.get("message"));
values.put("image", queryValues.get("image"));


return database.update("friends", values,
"contactId" + " = ?", new String[] {queryValues.get("contactId") });

}

public void deleteContact(){

SQLiteDatabase database = this.getWritableDatabase();

String deleteQuery = "DELETE FROM friends ";

database.execSQL(deleteQuery);

}

public ArrayList<HashMap<String, String>> getAllContacts(){

ArrayList<HashMap<String, String>> contactArrayList = new ArrayList<HashMap<String, String>>();


String selectQuery = "SELECT * FROM friends ";
SQLiteDatabase database = this.getWritableDatabase();

Cursor cursor = database.rawQuery(selectQuery, null);


if(cursor.moveToFirst()){

do{

HashMap<String, String> contactMap = new HashMap<String, String>();

contactMap.put("name", cursor.getString(0));
contactMap.put("message", cursor.getString(1));
contactMap.put("image", cursor.getString(2));


contactArrayList.add(contactMap);

} while(cursor.moveToNext());

}
return contactArrayList;

}

public HashMap<String, String> getContactInfo(String id){

HashMap<String, String> contactMap = new HashMap<String, String>();

SQLiteDatabase database = this.getReadableDatabase();

String selectQuery = "SELECT * FROM friends WHERE contactId='" + id + "'";

Cursor cursor = database.rawQuery(selectQuery, null);

if(cursor.moveToFirst()){

do{

contactMap.put("name", cursor.getString(0));
contactMap.put("message", cursor.getString(1));
contactMap.put("image", cursor.getString(2));

} while(cursor.moveToNext());

}

return contactMap;

}

}

这是我获取数据和昆虫的类

public class Chat extends SherlockListFragment {
JSONParser jsonParser = new JSONParser();
private static final String TAG_POSTS = "posts";
public static final String TAG_ID = "id";
public static final String TAG_NAME = "name";
public static final String TAG_pic = "pic";
public static final String TAG_MESSAGE = "message";
public static final String TAG_CATEGORIES_LOGO = "categories_logo";
DBTool dbTool = new DBTool(getActivity());
/*
user details
private static final String NAME = "name";
private static final String AGE = "age";
private static final String STATUS = "status";
private static final String PIC = "pic";
private static final String SEX = "sex"; String friendname,status;

private static final String TAG_SUCCESS = "success";*/
//user
private static final String URL_CATEGORY = "http://10.0.2.2/ochat/selectchat.php";

private ListView lv;
SharedPreferences sp ;
Bundle bon = new Bundle();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.friends, container, false);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

new LoadComments().execute();
}

class LoadComments extends AsyncTask<Void, Void, ArrayList<HashMap<String,String>>> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());

pDialog.setIndeterminate(false);
pDialog.setCancelable(true);

}

@Override
protected ArrayList<HashMap<String, String>> doInBackground(Void... arg0) {

ArrayList<HashMap<String, String>> categoryList = new ArrayList<HashMap<String, String>>();

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
String username = sp.getString("username", "anon");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
JSONObject json = jsonParser.makeHttpRequest(URL_CATEGORY, "POST",
params);

try {

JSONArray categories = json.getJSONArray(TAG_POSTS);
for (int i = 0; i < categories.length(); i++) {
String id = categories.getJSONObject(i).getString("TAG_ID");
String name = categories.getJSONObject(i).getString("TAG_NAME");
String pic = categories.getJSONObject(i).getString("TAG_pic");
String message = categories.getJSONObject(i).getString("TAG_MESSAGE");

HashMap<String, String> queryValuesMap = new HashMap<String, String>();

queryValuesMap.put("name", name);

queryValuesMap.put("message",message);

DefaultHttpClient mHttpClient = new DefaultHttpClient();
HttpGet mHttpGet = new HttpGet(pic);
HttpResponse mHttpResponse = mHttpClient.execute(mHttpGet);
if (mHttpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = mHttpResponse.getEntity();
if ( entity != null) {
// insert to database
queryValuesMap.put("image", EntityUtils.toByteArray(entity));

}
}


dbTool.insertContact(queryValuesMap);

}
}catch (Throwable e){
e.printStackTrace();
}
return categoryList;
}

@Override
protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
super.onPostExecute(result);


}
}
}

问题出在

queryValuesMap.put("image", EntityUtils.toByteArray(entity));

在说

The method put(String, String) in the type HashMap is not applicable for the arguments (String, byte[])

我不知道是我这样安排的还是有更好的办法.. 去的想法from this site .

最佳答案

问题是函数 EntityUtils.toByteArray() 返回的正是:byte[],但是你的 HashMap 需要一个字符串。

尝试将此行更改为

queryValuesMap.put("image", new String(EntityUtils.toByteArray(entity)));

构造函数String(byte[]) will create a new string from your bytes .

关于android - 从 urls 将图像存储在 Sqlite 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29448963/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com