【发布时间】:2014-10-13 00:16:19
【问题描述】:
正如标题所言。
这就是我想要做的事情
1) 打开图库(我知道怎么做) 2)从图库中选择图像 3)获取Image Id并将ID传递给另一个Activity 4)在这个其他活动中,我想将此图像加载到 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 = new Intent(this, NextActivity.class);
intent.putExtra("picture", byteArray);
startActivity(intent);
然后从Bundle中获取Byte Array并转换成Bitmap Image
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);
但我相信通过传递 ID 然后在下一个活动中获取 id 并从 SD 卡加载图像会更好地提高内存和性能。
【问题讨论】:
标签: android image android-intent bitmap android-gallery