【问题标题】:How to delete temporary image from internal storage如何从内部存储中删除临时图像
【发布时间】: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


【解决方案1】:

您可以使用File tmpBitmap = getFileStreamPath("tempfile_wip");,它返回文件系统上存储使用openFileOutput(String, int) 创建的文件的绝对路径,并使用tmpBitmap.delete()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-26
    • 2019-12-21
    • 1970-01-01
    相关资源
    最近更新 更多