[英]How to delete temporary image from internal storage
我不得不從一個活動傳遞到另一個位圖,但我得到了OutOfMemory錯誤,同時通過位圖作為字節流中intent.getParcelableExtra
,所以我在內部存儲臨時保存的圖像,並在我的目的地檢索活動它。
來源活動
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
,沒有絕對路徑。
您可以使用File tmpBitmap = getFileStreamPath("tempfile_wip");
它返回存儲使用openFileOutput(String, int)
創建的文件的文件系統上的絕對路徑,並使用tmpBitmap.delete()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.