简体   繁体   English

如何从内部存储删除临时映像

[英]How to delete temporary image from internal storage

I had to pass a bitmap from one activity to another, but I was getting OutOfMemory error while passing the bitmap as byteStream in intent.getParcelableExtra , so I saved the image temporarily in internal storage, and retrieved it in my destination activity. 我不得不从一个活动传递到另一个位图,但我得到了OutOfMemory错误,同时通过位图作为字节流中intent.getParcelableExtra ,所以我在内部存储临时保存的图像,并在我的目的地检索活动它。

Source Activity 来源活动

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();
                }

Destination activity 目的地活动

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();
    }

Since I have loaded the image in destination activity's ImageView I no longer need the temp image file. 由于我已将图像加载到目标活动的ImageView中,因此不再需要临时图像文件。 How can I delete this file from internal storage? 如何从内部存储删除此文件? I only have the filename tempfile_wip and do not have the absolute path. 我只有文件名tempfile_wip ,没有绝对路径。

you can use File tmpBitmap = getFileStreamPath("tempfile_wip"); 您可以使用File tmpBitmap = getFileStreamPath("tempfile_wip"); which returns the absolute path on the filesystem where a file created with openFileOutput(String, int) is stored, and use tmpBitmap.delete() 它返回存储使用openFileOutput(String, int)创建的文件的文件系统上的绝对路径,并使用tmpBitmap.delete()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM