13

I am looking out for a way to save a bitmap file temporarily in android file system. The file is required only until it is used as a part of POST request to a server after which I want it to cease to exist. I am looking for the faster way of doing this.

...
File file = new File(Environment.getExternalStorageDirectory().getPath().toString()+"/ImageDB/" + fileName+".png");
FileOutputStream filecon = new FileOutputStream(file);
sampleResized.compress(Bitmap.CompressFormat.JPEG, 90, filecon);
... 

I am currently using this method.

EDIT: I got my solution from Creating temporary files in Android

Community
  • 1
  • 1
Mohitt
  • 2,957
  • 3
  • 29
  • 52

4 Answers4

15
File f3=new File(Environment.getExternalStorageDirectory()+"/inpaint/");
if(!f3.exists())
    f3.mkdirs();        
OutputStream outStream = null;
File file = new File(Environment.getExternalStorageDirectory() + "/inpaint/"+"seconds"+".png");
try {
    outStream = new FileOutputStream(file);
    mBitmap.compress(Bitmap.CompressFormat.PNG, 85, outStream);
    outStream.close();
    Toast.makeText(getApplicationContext(), "Saved", Toast.LENGTH_LONG).show();
} catch (Exception e) {
    e.printStackTrace();
} 
Sergei Bubenshchikov
  • 5,275
  • 3
  • 33
  • 60
Don Chakkappan
  • 7,397
  • 5
  • 44
  • 59
4

Please check the below code. All the above codes are right. But if we compress JPEG it work fast as compare to PNG. So Better to use JPEG to imporove performance..

FileOutputStream fileOutputStream = new FileOutputStream(path);
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
viewCapture.compress(CompressFormat.JPEG, 50, bos);
bos.flush();
bos.close();

For Delete just use

File myFile = new File(path);
myFile.delete();

Hope its helpfull for you

Amr
  • 1,068
  • 12
  • 21
sharma_kunal
  • 2,152
  • 1
  • 28
  • 28
1

You can use file's file.delete() method , after closing filecon

 File file = new File(Environment.getExternalStorageDirectory().getPath().toString()+"/ImageDB/" + fileName+".png");
    FileOutputStream filecon = new FileOutputStream(file);
    sampleResized.compress(Bitmap.CompressFormat.JPEG, 90, filecon);
    if(filecon!null=) filecon.close;
    file.delete();
Mr.India
  • 674
  • 2
  • 9
  • 19
1

Get the response of your post and then add this into:

boolean deleted = file.delete();

You can get the confirmation of the deletion like this.

SteamFire
  • 367
  • 2
  • 14