Question:
When writing a file less than 2kb, the problem of insufficient space is directly reported. The file requested by retrofit2 is used. Can any expert have time to find out the reason?
Error report details:
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: Lyric file request successful
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
Code:
//Save lyrics file to disk
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;
}
为情所困2017-05-24 11:40:18
while (fileread!= -1){
out.write(b,0,fileread);
}
It is wrong to use a loop. This loop should be changed to if