0
点赞
收藏
分享

微信扫一扫

saveFileToSD

public int saveFileToSD(InputStream is, String sPath) {

if(null == sPath || 0 == sPath.trim().length()) return HttpDownMgrUrl.FILE_PATH_ERROR;

if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {// 判断SD卡是否可用

return HttpDownMgrUrl.NOT_WRITE_DEVICE;

}


android.util.Log.i("HttpDownMgrTest", "HttpDownMgrComm=>saveFileToSD=>sPath="+sPath);


String strPath = sPath+".tmp";

File file = new File(strPath);

int iRet = HttpDownMgrUrl.SUCCESS;

try{

if(file.exists()) file.delete();

if (!file.isFile()) {

OutputStream oStream = null;

try {

file.createNewFile();

oStream = new FileOutputStream(file);

byte[] buffer = new byte[10240];

int len = 0;

while ((len = is.read(buffer)) != -1) {

oStream.write(buffer, 0, len);

try{Thread.sleep(10);}catch(Exception eee){}

}

oStream.flush();

oStream.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

iRet = HttpDownMgrUrl.FILE_CREATE_ERROR;

} catch (IOException e) {

e.printStackTrace();

iRet = HttpDownMgrUrl.FILE_IO_ERROR;

}finally{


if(null!=oStream){try{oStream.close();}catch(Exception e){}finally{oStream = null;}}

}

}

if (iRet ==HttpDownMgrUrl.SUCCESS ) {

File newfile = new File(sPath);

newfile.delete();

file.renameTo(newfile);

}

}catch(SecurityException ee){

ee.printStackTrace();

iRet = HttpDownMgrUrl.FILE_SECURITY_ERROR;

}

return iRet;

}

举报
0 条评论