java - 使用retrofit2下载歌词文件后,进行保存时报空间不足的错误
大家讲道理
大家讲道理 2017-05-24 11:38:18
0
1
1059

问题:
写入一个不到2kb的文件时直接报空间不足的问题,使用的是retrofit2请求的文件,哪位大神有时间给看看是什么原因吧

报错的详情:
D/OkHttp: --> GET http://musicdata.baidu.com/da... http/1.1
D/OkHttp: <-- 200 OK http://musicdata.baidu.com/da... (98ms, 1877-byte body)
D/ContentValues: onResponse: 歌词文件请求成功
I/art: Thread[2,tid=3036,WaitingInMainSignalCatcherLoop,Thread*=0xae402000,peer=0x12c930a0,"Signal Catcher"]: reacting to signal 3
I/art: Wrote stack traces to '/data/anr/traces.txt'
W/System.err: java.io.IOException: write failed: ENOSPC (No space left on device)
W/System.err: at libcore.io.IoBridge.write(IoBridge.java:498)
W/System.err: at java.io.FileOutputStream.write(FileOutputStream.java:186)
W/System.err: at com.wang.mymusic.data.MusicData.lrcFile(MusicData.java:163)
W/System.err: at com.wang.mymusic.data.MusicData.access$200(MusicData.java:40)
W/System.err: at com.wang.mymusic.data.MusicData$3.onResponse(MusicData.java:123)
W/System.err: at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err: at android.os.Looper.loop(Looper.java:148)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5417)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
W/System.err: Caused by: android.system.ErrnoException: write failed: ENOSPC (No space left on device)
W/System.err: at libcore.io.Posix.writeBytes(Native Method)
W/System.err: at libcore.io.Posix.write(Posix.java:271)
W/System.err: at libcore.io.BlockGuardOs.write(BlockGuardOs.java:313)
W/System.err: at libcore.io.IoBridge.write(IoBridge.java:493)
W/System.err: ... 12 more
I/art: Background partial concurrent mark sweep GC freed 83995(3MB) AllocSpace objects, 0(0B) LOS objects, 12% free, 27MB/31MB, paused 3.141ms total 131.690ms
I/art: Background sticky concurrent mark sweep GC freed 104777(4MB) AllocSpace objects, 0(0B) LOS objects, 0% free, 34MB/34MB, paused 5.311ms total 163.136ms

代码:

//保存歌词文件到磁盘

private File lrcFile(Context context,ResponseBody respon,String title,String author) throws IOException {
    //create a file
    File filedirectory;
    File file;
    String name=title+"_"+author+".lrc";
    if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !Environment.isExternalStorageRemovable()){
        filedirectory=new File(context.getExternalCacheDir()+File.separator+"lrc");
        file=new File(filedirectory+File.separator+name);
    }else {
        filedirectory=new File(context.getCacheDir()+File.separator+"lrc");
        file=new File(filedirectory+File.separator+name);
    }
    if (!filedirectory.exists()){
        filedirectory.mkdir();
    }
    if(!file.exists()){
        file.createNewFile();
    }
    InputStream in=respon.byteStream();
    try {
        OutputStream out=new FileOutputStream(file);
        byte[] b=new byte[2048];
        int fileread=in.read(b);
        while (fileread!= -1){
            out.write(b,0,fileread);
        }
        out.flush();
        out.close();
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return file;
}
大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

全部回复(1)
为情所困

while (fileread!= -1){

        out.write(b,0,fileread);
    }

这个使用循环是不对的,这块应该将循环改成if

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!