gpt4 book ai didi

java - 将 Drawable 转换为 BLOB 数据类型

转载 作者:行者123 更新时间:2023-12-02 08:17:17 25 4
gpt4 key购买 nike

public Drawable icon; \\ Some value in this field

我正在 SQLite Manager 中创建数据库。我想将 icon 存储到数据库中。另一个字段显示正确,但是当我获取数据类型为 BLOBicon 字段时,它不显示。

public void prettyPrint() {
public String appname = "asd";
public String pname = "asd";
public String versionName = "asd";
public int versionCode = 0;
public Drawable icon="android.im....";
Insall_app_db i1 = new Insall_app_db();
i1.createDatabse("appInfo3", getBaseContext());
i1.createTable("off_appinfo4", getBaseContext()); i1.insertDataUser("off_appinfo4", appid ,appname,pname, versionName, versionCode, icon, x);
}


package com.AppInfo;

import java.sql.Blob;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.R.drawable;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.drawable.Drawable;

public class Insall_app_db {


SQLiteDatabase sampleDB = null;
String PhoneNumber;
String UserId;
Cursor c;
ArrayList ar=new ArrayList();
public void createDatabse(String DbName,Context context)
{
sampleDB = context.openOrCreateDatabase(DbName,
context.MODE_WORLD_READABLE,null);

}

public void createTable(String Tablename,Context context)
{
sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " +
Tablename +
"(app_id INT,app_name VARCHAR(255),pname VARCHAR(255),version_name VARCHAR(25),versionCode INT,icon BLOB,date DATE);");

}
public void DeleteTable(String Tablename,Context context)
{
sampleDB.execSQL("DROP TABLE off_appinfo4");

}

public void insertDataUser(String tableName,int app_id,String app_name,String pname,String version_name,int versionCode,String d,long date1)
{

sampleDB.execSQL("INSERT INTO " +
tableName +
" Values ("+app_id+",'"+app_name+"','"+pname+"','"+version_name+"','"+versionCode+"','"+d+"',"+date1+");");


}

public String GetUserData(Context context,String tablename)
{
c = sampleDB.rawQuery("SELECT User_Id FROM " +
tablename +
"", null);

if(c!=null)
{
if(c.moveToFirst())
{
do
{
UserId=c.getString(c.getColumnIndex("User_Id"));

}while(c.moveToNext());



}

}

return UserId;
}

public void close()
{
sampleDB.close();

}




}

最佳答案

您可以将图像转换为字节数组,并将该字节数组存储在数据库中的 blob 字段中。您可以从数据库中以字节数组的形式检索图像。

如何从imageview获取字节数组:

ImageView iv = (ImageView) findViewById(R.id.splashImageView);
Drawable d = iv.getBackground();
BitmapDrawable bitDw = ((BitmapDrawable) d);
Bitmap bitmap = bitDw.getBitmap();
System.out.println(".....d....."+d);
System.out.println("...bitDw...."+bitDw);
System.out.println("....bitmap...."+bitmap);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] imageInByte = stream.toByteArray();

将字节数组存储在数据库中:

ContentValues cv = new ContentValues();
cv.put(CHUNK, buffer); //CHUNK blob type field of your table
long rawId = database.insert(TABLE, null, cv); //TABLE table name

从字节数组中检索图像:

public static Bitmap convertByteArrayToBitmap(
byte[] byteArrayToBeCOnvertedIntoBitMap)
{
bitMapImage = BitmapFactory.decodeByteArray(
byteArrayToBeCOnvertedIntoBitMap, 0,
byteArrayToBeCOnvertedIntoBitMap.length);
return bitMapImage;
}

关于java - 将 Drawable 转换为 BLOB 数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6341977/

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