[英]Android: download and save image in SD. Error when i try open file before writing finished
我正在嘗試使用AsyncTask
在后台將圖像列表下載並保存到Internet上的SD卡中,這很好。
另一方面,在主代碼中,我將檢查SD卡上是否存在圖像。 如果存在,則在本地加載圖像,如果不存在,則加載URL圖像。
當文件圖像存在但寫入未完成時,出現問題。 在這種情況下,Web視圖將加載白頁。
我已經嘗試過了,但是問題是我不知道文件大小,並且圖像大小不同。
if(file.exists() && file.length() > 18000){
//Load SD file
}
else{
//Load url
}
有沒有辦法知道文件寫入是否完成?
就在這里。
嘗試這個
final int giveUpTimeout = 5;
new Timer().schedule( new TimerTask() {
public void run() {
try{
int cc = giveUpTimeout;
while ( cc > 0 ) {
sleep(1000); // wait 1 sec
cc --;
if ( file.exists() && file.lenght()>0 ) {
...
// LOAD sd FILE
return ; // should exit
}
}
// your else
... LOAD URL
}
catch ( Exception e) { e.printStackTrace(); }
}
},
0);
bjarn =]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.