您的位置:首页 > 移动开发 > Android开发

retrofit下载大文件注意点和android存储位置

2017-01-19 13:09 375 查看
1.retrofit下载大文件需要注意的点:(1)下载代码
try{
int fileSize = mAttachmentFiles.get(index).getFileSize();
InputStream inputStream = null;
OutputStream outputStream = null;
int fileSizeDownloaded = 0;
inputStream = body.byteStream();
try {
byte[] fileReader = new byte[2048];
outputStream = new FileOutputStream(file);
while (true) {
int read = inputStream.read(fileReader);
if (read == -1) {
mHandler.sendEmptyMessage(-1);
turnByIntent();
break;
}
outputStream.write(fileReader, 0, read);
fileSizeDownloaded += read;
int num =(int) (((float)fileSizeDownloaded/ (float) fileSize)*100);
Message msg = mHandler.obtainMessage();
msg.arg1 = num;
mHandler.sendMessage(msg);
}
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
} catch (IOException e) {
e.printStackTrace();
}
(2)@Streaming,不能丢,亲测,不加这个注解,会报oom.
2.andoid存储目录:
//Context.getExternalFilesDir() --> SDCard/Android/data/你的应用的包名/files/ 目录,一般放一些长时间保存的数据9287//Context.getExternalCacheDir() --> SDCard/Android/data/你的应用包名/cache/目录,一般存放临时缓存数据  
getCacheDir()方法用于获取/data/data/<application package>/cache目录getFilesDir()方法用于获取/data/data/<application package>/files目录
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: