[英]Reading an image as byte array with Android: skImageDecoder::Factory returned null
[英]Android: Bitmap to Byte Array and back: SkImageDecoder::Factory returned null
目標是將Bitmap
轉換為byte []
,在數據Bundle
的活動之間傳遞它,然后在稍后階段將其重新轉換回Bitmap
以在Imageview
顯示。
問題在於,每當我嘗試這個時,我只得到一個空位圖和非描述性,無用的日志輸出:
12-07 17:01:33.282: D/skia(2971): --- SkImageDecoder::Factory returned null
我看過以下解決方案:
突出顯示copyPixelsToBuffer()對.compress至關重要
(特別是因為在這種情況下沒有必要)。
我已經運行了以下測試用例,這肯定會將問題縮小到轉換和恢復代碼。 基於我的調試,有正確的解碼,字節數組是正確的大小和完整,位圖配置被迫是相同的, decodeByteArray
只是失敗:
package com.example.debug;
import java.nio.ByteBuffer;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
RelativeLayout rl = null;
RelativeLayout.LayoutParams rlp = null;
ImageView ivBef = null;
ImageView ivAft = null;
Bitmap bmBef = null;
Bitmap bmAft = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TEST
BitmapFactory.Options bmo = new BitmapFactory.Options();
bmo.inPreferredConfig = Config.ARGB_8888;
bmBef = BitmapFactory.decodeFile("/mnt/sdcard/Debug/001.png", bmo);
byte[] b = bitmapToByteArray(bmBef);
bmAft = BitmapFactory.decodeByteArray(b, 0, b.length, bmo);
LinearLayout ll = new LinearLayout(this);
ivBef = new ImageView(this);
ivBef.setImageBitmap(bmBef);
ivAft = new ImageView(this);
ivAft.setImageBitmap(bmAft);
ll.addView(ivBef);
ll.addView(ivAft);
setContentView(ll);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public static byte[] bitmapToByteArray(Bitmap bm) {
// Create the buffer with the correct size
int iBytes = bm.getWidth() * bm.getHeight() * 4;
ByteBuffer buffer = ByteBuffer.allocate(iBytes);
// Log.e("DBG", buffer.remaining()+""); -- Returns a correct number based on dimensions
// Copy to buffer and then into byte array
bm.copyPixelsToBuffer(buffer);
// Log.e("DBG", buffer.remaining()+""); -- Returns 0
return buffer.array();
}
}
之前的Imageview
正確顯示圖像,之后ImageView
沒有顯示任何內容(正如您所期望的那樣,使用Imageview
圖
您正在將Bitmap傳遞給Intent並從bundle中獲取下一個活動中的位圖,但問題是如果您的位圖/圖像大小很大,那么圖像不會在下一個活動中加載。
使用以下2解決方案來解決此問題。
1)首先將圖像轉換為字節數組然后傳入Intent並在下一個活動中從Bundle獲取字節數組並轉換為圖像(位圖)並設置為ImageView。
將位圖轉換為字節數組: -
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
將字節數組傳遞給intent: -
Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("picture", byteArray);
startActivity(intent);
從Bundle獲取字節數組並轉換為位圖圖像: -
Bundle extras = getIntent().getExtras();
byte[] byteArray = extras.getByteArray("picture");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(bmp);
2)首先將圖像保存到SD卡中,然后在下一個活動中將此圖像設置為ImageView。
以下方法與我完美配合,試一試..
public byte[] convertBitmapToByteArray(Context context, Bitmap bitmap) {
ByteArrayOutputStream buffer = new ByteArrayOutputStream(bitmap.getWidth() * bitmap.getHeight());
bitmap.compress(CompressFormat.PNG, 100, buffer);
return buffer.toByteArray();
}
試試這個:
bmBef = BitmapFactory.decodeFile("/mnt/sdcard/Debug/001.png", bmo);
ByteArrayOutputStream baos= new ByteArrayOutputStream();
bmBef .compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] byteArray = baos.toByteArray();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.