2014-04-01 106 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(); 

希望它有幫助你