如何使用包在Android活动之间传递图像(位图)?假设我有一个活动从图片库中选择一个图像,并以位图的形式检索它,就像下面的例子:这里现在,我想传递这个位图,以便在另一个活动的ImageView中使用。我知道包可以在活动之间传递,但是如何将这个位图存储到包中呢?还是我应该采取另一种方法?
3 回答
慕斯王
TA贡献1864条经验 获得超2个赞
intent.putExtra("data", bitmap)putParcelable.
大话西游666
TA贡献1817条经验 获得超14个赞
Intent i = new Intent(this, NextActivity.class);Bitmap b;
// your bitmapByteArrayOutputStream bs = new ByteArrayOutputStream();b.compress(Bitmap.CompressFormat.PNG, 50, bs);
i.putExtra("byteArray", bs.toByteArray());startActivity(i);if(getIntent().hasExtra("byteArray")) {
ImageView previewThumbnail = new ImageView(this);
Bitmap b = BitmapFactory.decodeByteArray(
getIntent().getByteArrayExtra("byteArray"),0,getIntent().getByteArrayExtra("byteArray").length);
previewThumbnail.setImageBitmap(b);}
qq_花开花谢_0
TA贡献1835条经验 获得超7个赞
public String createImageFromBitmap(Bitmap bitmap) {
String fileName = "myImage";//no .png or .jpg needed
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
FileOutputStream fo = openFileOutput(fileName, Context.MODE_PRIVATE);
fo.write(bytes.toByteArray());
// remember close file output
fo.close();
} catch (Exception e) {
e.printStackTrace();
fileName = null;
}
return fileName;}Bitmap bitmap = BitmapFactory.decodeStream(context
.openFileInput("myImage"));//here context can be anything like getActivity() for fragment, this or MainActivity.this注
- 3 回答
- 0 关注
- 655 浏览
添加回答
举报
0/150
提交
取消
