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

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目录


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值