B T*_*B T 14 java android bytearray bitmap android-imageview
目标是将a转换Bitmap为a byte [],在Bundle数据中的活动之间传递,然后将其重新转换回Bitmap稍后阶段以便在a中显示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();
}
}
Run Code Online (Sandbox Code Playgroud)
之前Imageview正确显示图像,后面ImageView没有显示任何内容(正如您所期望的那样,使用空位图
Dip*_*iya 49
您正在将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();
Run Code Online (Sandbox Code Playgroud)
将字节数组传递给intent: -
Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("picture", byteArray);
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
从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);
Run Code Online (Sandbox Code Playgroud)
2)首先将图像保存到SD卡中,然后在下一个活动中将此图像设置为ImageView.
| 归档时间: |
|
| 查看次数: |
64111 次 |
| 最近记录: |