【发布时间】:2015-05-16 12:14:14
【问题描述】:
我必须将位图从一个活动传递到另一个活动,但在将位图作为 byteStream 在intent.getParcelableExtra 中传递时出现 OutOfMemory 错误,因此我将图像临时保存在内部存储中,并在我的目标活动中检索它。
源活动
Bitmap btmp = //my bitmap here.
String fileName = "tempfile_wip";
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
btmp.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
FileOutputStream fo = openFileOutput(fileName, Context.MODE_PRIVATE);
fo.write(bytes.toByteArray());
fo.close();
Intent i = new Intent(getApplicationContext(), apply_effects.class);
startActivity(i);
} catch (Exception e) {
Toast.makeText(context, "Error 010", Toast.LENGTH_SHORT).show();
}
目标活动
try {
ImageView image = (ImageView) findViewById(R.id.iv_effect);
Bitmap bitmap = BitmapFactory.decodeStream(this.getApplicationContext().openFileInput("tempfile_wip"));
image.setImageBitmap(bitmap);
}
catch(Exception ex) {
Toast.makeText(this.getApplicationContext(), "Error 009 occurred.", Toast.LENGTH_SHORT).show();
}
由于我已经在目标活动的 ImageView 中加载了图像,因此我不再需要临时图像文件。如何从内部存储中删除此文件?我只有文件名tempfile_wip,没有绝对路径。
【问题讨论】:
-
从哪里获得 bitamp 尝试从另一个活动中获得相同。
-
这是一个从头开始创建的位图。有点像照片拼贴应用程序,其中最终图像是位图
标签: android