Android暂时保存位图图像

时间:2014-04-01 09:12:16

标签: java android file bitmap

我正在寻找一种在android文件系统中临时保存位图文件的方法。只有在将文件用作服务器的POST请求的一部分之后才需要该文件,之后我希望它不再存在。我正在寻找更快的方法。

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

我目前正在使用此方法。

编辑:我从Creating temporary files in Android

获得了我的解决方案

4 个答案:

答案 0 :(得分:11)

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

答案 1 :(得分:2)

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

希望对你有所帮助

答案 2 :(得分:1)

关闭file.delete()

后,您可以使用文件的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();

答案 3 :(得分:1)

获取帖子的回复,然后将其添加到:

boolean deleted = file.delete();

你可以像这样得到删除的确认。