问题:
写入一个不到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;
}
while (fileread!= -1){
这个使用循环是不对的,这块应该将循环改成if