2014-04-01 108 views
7

我正在寻找一种在android文件系统中临时保存位图文件的方法。该文件是必需的,直到它被用作服务器POST请求的一部分后,我希望它不再存在。我正在寻找更快的方式来做到这一点。Android临时保存位图图像

... 
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

回答

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

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

让你的帖子的反应,然后添加到这个后:

boolean deleted = file.delete();

您可以像这样获得删除的确认。

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

希望它有帮助你