使用以下为字节数组方法转换图像,并存储到数据库中的BLOB
public String bitmapToString(Bitmap bmp) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, os);
byte[] bytes = os.toByteArray();
return Base64.encodeToString(bytes, Base64.DEFAULT);
}
而当你想显示成图像视图中使用滑翔使用以下
public void stringToBitmap(String str) {
byte[] bytesImage = Base64.decode(str, Base64.DEFAULT);
//Bitmap bitmap = BitmapFactory.decodeByteArray(bytesImage, 0, bytesImage.length);
//ivImage.setImageBitmap(bitmap);
// using glide
Glide.with(this).load(bytesImage).asBitmap().into(ivImage);
}
存储的简单例子并从数据库检索图像
activity_main.xml
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:id="@+id/btnInsert"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Insert into DB" />
android:id="@+id/btnView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="View from DB" />
android:id="@+id/ivImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
MainActivity.java
public class MainActivity extends AppCompatActivity {
private DatabaseHelper databaseHelper;
private SQLiteDatabase db;
private Button btnInsert, btnView;
private ImageView ivImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
databaseHelper = new DatabaseHelper(this);
db = databaseHelper.getWritableDatabase();
ivImage = (ImageView) findViewById(R.id.ivImage);
btnInsert = (Button) findViewById(R.id.btnInsert);
btnView = (Button) findViewById(R.id.btnView);
btnInsert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
insertImage();
}
});
btnView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
viewFromDb();
}
});
}
private void insertImage() {
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ContentValues values = new ContentValues();
values.put("image", bitmapToString(bmp));
db.insert("table_image", null, values);
Toast.makeText(this, "Insert Successful",Toast.LENGTH_SHORT).show();
}
private void viewFromDb() {
String sql = "select * from table_image";
ArrayList images = new ArrayList<>();
Cursor cursor = db.rawQuery(sql, null);
if (cursor != null) {
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
String img = cursor.getString(cursor.getColumnIndex("image"));
images.add(img);
cursor.moveToNext();
}
}
cursor.close();
if (images.size() > 0) {
// get last image
stringToBitmap(images.get(images.size()-1));
}
}
public String bitmapToString(Bitmap bmp) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, os);
byte[] bytes = os.toByteArray();
//return Base64.encodeToString(bytes, Base64.DEFAULT);
return Base64.encodeToString(bytes, Base64.DEFAULT);
}
public void stringToBitmap(String str) {
byte[] bytesImage = Base64.decode(str, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(bytesImage, 0, bytesImage.length);
ivImage.setImageBitmap(bitmap);
//Glide.with(this).load(bytesImage).asBitmap().into(ivImage);
}
}
DatabaseHelper.java
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String TAG = "DatabaseHelper";
public static String DB_PATH;
public static String DB_NAME = "mdb";
private static SQLiteDatabase myDataBase;
private static DatabaseHelper helperInstance;
private final Context myContext;
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
DB_PATH = "/data/data/" + myContext.getPackageName() + "/databases/";
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.e(TAG, "On Create");
String sql = "CREATE TABLE IF NOT EXISTS table_image (_id INTEGER PRIMARY KEY AUTOINCREMENT, image BLOB)";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
我希望这是有用的给你。