我正在尝试在ImageView上绘制一个Bitmap,但它没有显示...我创建了Bitmap并存储在Intent Extra上,但是在新活动中我无法在ImageView上绘制它
这是我的代码,调用新活动:
public void onClickShare(View v) {
Intent myIntent = new Intent(this, Share.class);
Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
myIntent.putExtra("createdImg", b);
startActivity(myIntent);
}
关于新活动:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.share);
backgroundImg = (ImageView)findViewById(R.id.imageView1);
Intent myIntent = getIntent();
Bitmap bitmap = (Bitmap) myIntent.getParcelableExtra("createdImg");
backgroundImg.setImageBitmap(bitmap);
}
我错过了什么?谢谢!
答案 0 :(得分:0)
尝试将其作为ByteArray发送并在接收活动中对其进行解码。
ByteArrayOutputStream stream = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Intent in1 = new Intent(this, Share.class);
in1.putExtra("image",byteArray);
在第二项活动中
byte[] byteArray = getIntent().getByteArrayExtra("image");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
答案 1 :(得分:0)
您也可以尝试这样
Bundle bundle = new Bundle();
bundle.putParcelable("createdImg", b);
intent.putExtras(bundle);
并在下一个活动中
Bundle bundle = this.getIntent().getExtras();
Bitmap bmap = bundle.getParcelable("createdImg");
在本教程http://android-er.blogspot.com/2011/10/pass-bitmap-between-activities.html
中给出答案 2 :(得分:0)
我从每个答案和其他研究中得到了一点点,我想出了下面的代码,它运作得很好:
调用新活动:
public void onClickShare(View v) {
Intent myIntent = new Intent(this, Share.class);
// create bitmap screen capture
Bitmap bitmap;
View v1 = v.getRootView();
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
// create a new one with the area I want
Bitmap bmpLast = Bitmap.createBitmap(bitmap, 25, 185, 430, 520);
v1.setDrawingCacheEnabled(false);
//create an empty bitmap with the size I need
Bitmap bmOverlay = Bitmap.createBitmap(480, 760, Bitmap.Config.ARGB_8888);
//draw the content on the bitmap
Canvas c = new Canvas(bmOverlay);
c.drawBitmap(bmpLast, 0, 0, null);
//compress the bitmap to send to other activity
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmpLast.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
//store compressed bitmap
myIntent.putExtra("createdImg", byteArray);
startActivity(myIntent);
}
关于新活动:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.share);
//decompress the bitmap
byte[] byteArray = getIntent().getByteArrayExtra("createdImg");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
//set the imageview with the bitmap
backgroundImg = (ImageView)findViewById(R.id.imageView1);
backgroundImg.setImageDrawable(new BitmapDrawable(getResources(), bmp));
}
谢谢大家! =)