Android临时保存位图图像

我正在寻找一种方法来在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中创build临时文件得到我的解决scheme

Solutions Collecting From Web of "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(); } 

closuresfilecon后,可以使用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();

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

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

希望对你有所帮助