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/ 目录,一般放一些长时间保存的数据 //Context.getExternalCacheDir() --> SDCard/Android/data/你的应用包名/cache/目录,一般存放临时缓存数据getCacheDir()方法用于获取/data/data/<application package>/cache目录
getFilesDir()方法用于获取/data/data/<application package>/files目录