【问题标题】:Android saving bitmap image temporarilyAndroid临时保存位图图像
【发布时间】:2014-05-11 23:08:21
【问题描述】:
我正在寻找一种将位图文件临时保存在 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
【问题讨论】:
标签:
java
android
file
bitmap
【解决方案1】:
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();
}
【解决方案2】:
请检查以下代码。以上所有代码都是正确的。但是如果我们压缩 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();
希望对你有帮助
【解决方案3】:
关闭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();
【解决方案4】:
获取您帖子的回复,然后将其添加到:
boolean deleted = file.delete();
你可以像这样得到删除的确认。