Android 临时保存位图图像

13

我正在寻找一种在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);
... 

我目前正在使用这种方法。

编辑:我从创建Android临时文件获取了我的解决方案。

4个回答

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

4
请查看下面的代码。所有以上代码都是正确的。但如果我们压缩JPEG,它会比PNG快。因此最好使用JPEG来提高性能。
FileOutputStream fileOutputStream = new FileOutputStream(path);
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
viewCapture.compress(CompressFormat.JPEG, 50, bos);
bos.flush();
bos.close();

要删除只需使用

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

希望能对您有所帮助


1
获取您的帖子响应,然后将其添加到:
boolean deleted = file.delete();
您可以像这样获得删除的确认。

1
您可以在关闭filecon后使用文件的file.delete()方法。
 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();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接