繁体   English   中英

Android暂时保存位图图像

[英]Android saving bitmap image temporarily

我正在寻找一种在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中创建临时文件获得了我的解决方案

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

希望对您有所帮助

关闭filecon后,可以使用file的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();

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

boolean deleted = file.delete();

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM