java 将 Drawable 转换为 BLOB 数据类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6341977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert Drawable to BLOB Datatype
提问by Nikunj Patel
public Drawable icon; \ Some value in this field
I am creating a database in SQLite Manager. I want to store icon
into the database. Another field is shown correctly, but when I take the icon
field with datatype BLOB
, it doesn't display.
我正在 SQLite 管理器中创建一个数据库。我想存储icon
到数据库中。另一个字段显示正确,但是当我icon
使用 datatype获取字段时BLOB
,它不显示。
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();
}
}
回答by Sunil Kumar Sahoo
You can convert your image into byte array and store that bytearray in blob filed in database. You can retrieve your image back from database as byte array.
您可以将图像转换为字节数组并将该字节数组存储在数据库中的 blob 文件中。您可以从数据库中检索您的图像作为字节数组。
How to get byte array from imageview:
如何从 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();
Store byte array in database:
在数据库中存储字节数组:
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
Retrieve image from byte array:
从字节数组中检索图像:
public static Bitmap convertByteArrayToBitmap(
byte[] byteArrayToBeCOnvertedIntoBitMap)
{
bitMapImage = BitmapFactory.decodeByteArray(
byteArrayToBeCOnvertedIntoBitMap, 0,
byteArrayToBeCOnvertedIntoBitMap.length);
return bitMapImage;
}
回答by Sandy
I made few changes in above answer to get byte array imageview
我在上面的答案中做了一些更改以获得字节数组 imageview
ImageView iv = (ImageView) findViewById(R.id.splashImageView);
ImageView iv = (ImageView) findViewById(R.id.splashImageView);
iv.setDrawingCacheEnabled(true);
Bitmap bitmap = iv.getDrawingCache();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] bitmapdata = stream.toByteArray();
回答by murli
if you want to save multiple images to database then you can create own method as following : //retrieve images from drawable into int array:
如果要将多个图像保存到数据库,则可以创建自己的方法,如下所示://将图像从 drawable 检索到 int 数组中:
int[] IMAGES = { R.drawable.yourimage1, R.drawable.yourimage2, R.drawable.yourimage3,
R.drawable.yourimage4, R.drawable.yourimage5 };
//create method to convert drawable images(which are in int) to byte[] as below:
public byte[] convertToByteArray(int image){
Resources resources = getResources();
Drawable drawable = resources.getDrawable(image);
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress( Bitmap.CompressFormat.PNG, 100, stream);
byte[] bitmapData = stream.toByteArray();
return bitmapData;
}
and now call this method as below: byte [] image = convertToByteArray(IMAGES[position]);// dbAdapter.insertValues(image);
现在调用这个方法如下: byte [] image = convertToByteArray(IMAGES[position]);// dbAdapter.insertValues(image);