gpt4 book ai didi

android - 将 url 中的图像插入到 Sqlite 表中

转载 作者:行者123 更新时间:2023-11-29 22:38:12 26 4
gpt4 key购买 nike

我正在尝试使用 blob 将图像插入到 sqlite 数据库中,我不想将图像存储在存储卡中并从中读取,因为我不希望用户有权访问它。我的表中有一行名为 image 。数据来自 mysql 数据库

package com.mall.our;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.HashMap;

import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.preference.PreferenceManager;
import android.util.Log;

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

}

}

这是我的另一个类

package com.mall.our;

import java.io.IOException;

import java.net.URL;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockListFragment;

import com.mall.first.JSONParser;
import com.mall.first.R;


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 = "http://10.0.2.2/ochat/chattingname.php";
private static final String URL_CATEGORY = "http://10.0.2.2/ochat/selectchat.php";

private BaseAdapter mAdapter;
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("image", pic);
queryValuesMap.put("message",message);

dbTool.insertContact(queryValuesMap);


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

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


}
}

}

我的问题是如何将 Chat.java 类中的“String pic”变量转换为图像数组或 url。我如何将其转换为位图,以便我可以将其插入表格中?

最佳答案

在此代码下方使用 ArrayList 并将每个图像 url 添加到其中。

ArrayList<String> list = new ArrayList<String>(); list.add(pic);

使用此代码将可绘制对象转换为位图。

ByteArrayOutputStream baos = new ByteArrayOutputStream();
// converting drawable to bitmap
_bitmapToInsert = ((BitmapDrawable) getResources().getDrawable(
R.drawable.skholingua)).getBitmap();
// compressing bitmap
_bitmapToInsert.compress(Bitmap.CompressFormat.PNG, 100, baos);
// converting bitmap to byte array
_photoToInsert = baos.toByteArray();
// inserting into to database
_sqlLiteManger.insert(_photoToInsert);

更多信息请阅读:http://www.skholingua.com/android-basic/advance-topics/database-storage

关于android - 将 url 中的图像插入到 Sqlite 表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29471073/

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