Mohitt
Mohitt

Reputation: 2977

Android saving bitmap image temporarily

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

Upvotes: 13

Views: 23254

Answers (4)

sharma_kunal
sharma_kunal

Reputation: 2192

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

Upvotes: 4

Don Chakkappan
Don Chakkappan

Reputation: 7560

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

Upvotes: 16

SteamFire
SteamFire

Reputation: 407

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.

Upvotes: 1

Mr.India
Mr.India

Reputation: 674

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

Upvotes: 1

Related Questions