简体   繁体   中英

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.

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. How can I delete this file from internal storage? I only have the filename tempfile_wip and do not have the absolute path.

you can use 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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