1) 作为extras
传递intent
在 Activity A 中,您将图像解码并通过 Intent 发送:
- 使用此方法(额外)图像在 162 毫秒的时间间隔内传递
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
Intent intent = new Intent(this, ActivityB.class);
intent.putExtra("picture", b);
startActivity(intent);
在 Activity B 中,您会收到带有字节数组(解码图片)的意图,并将其作为源应用到 ImageView:
Bundle extras = getIntent().getExtras();
byte[] b = extras.getByteArray("picture");
Bitmap bmp = BitmapFactory.decodeByteArray(b, 0, b.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(bmp);
2)保存图像文件并将其引用传递给另一个活动
"大小限制是:尽量小。绝对不要放
除非它不大于图标(32x32 或
随便)。
- 在*Activity A* 中保存文件(内部存储)
String fileName = "SomeName.png";
try {
FileOutputStream fileOutStream = openFileOutput(fileName, MODE_PRIVATE);
fileOutStream.write(b); //b is byte array
//(used if you have your picture downloaded
// from the *Web* or got it from the *devices camera*)
//otherwise this technique is useless
fileOutStream.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
Intent intent = new Intent(this, ActivityB.class);
intent.putExtra("picname", fileName);
Bundle extras = getIntent().getExtras();
String fileName = extras.getString("picname");
File filePath = getFileStreamPath(fileName);
Drawable d = Drawable.createFromPath(filePath.toString());
someImageView.setBackgroundDrawable(d);