[英]Convert Byte to Bitmap
我必須將一個字節轉換為位圖並將其設置為imageview
我確實有將ImageView中的位圖轉換為Byte的方法,稍后再插入,
public static byte[] ConvertDrawableToByteArray(Drawable drawableResource) {
Bitmap imageBitmap = ((BitmapDrawable) drawableResource).getBitmap();
ByteArrayOutputStream imageByteStream = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, imageByteStream);
byte[] imageByteData = imageByteStream.toByteArray();
return imageByteData;
}
當我想從數據庫中檢索圖像並將其顯示在ImageView中時
//--
byte[] image_b = c.getBlob(4);
Bitmap b = BitmapFactory.decodeByteArray(image_b, 0, image_b.length);
img.setImageBitmap(b);
但它什么也沒返回
有什么問題,請幫忙
非常感謝
我猜這是因為游標沒有返回任何東西。 也許Blob太大而無法一次查詢,因為每個查詢的android游標限制為1 mb,請再次檢查您的stacktrace / logcat,如果blob大小有問題,您可以嘗試將字節拆分為較小的大小並將其存儲在您的數據庫
您可以嘗試以下操作,並讓我們知道它是否有效:
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = resolver.query(imageUri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
return BitmapFactory.decodeFile(picturePath);
我不明白我在哪里喜歡這個問題:字符串t = image_b.toString(); System.out.println(“給我的東西” + t); 我在logcat中發現以下行:03-19 17:01:28.167:I / System.out(1019):它給了我什么[B @ 41f1f5c0
就是說它返回了什么但不顯示,或者字節[B@41f1f5c0
不正確
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.