D/StrictMode( 9730): StrictMode policy violation; ~duration=20 ms: android.os.StrictMode$StrictModeDiskReadViolation: policy=31 violation=2
D/StrictMode( 9730): at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1176)D/StrictMode( 9730): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:106)D/StrictMode( 9730): at libcore.io.IoBridge.open(IoBridge.java:390)D/StrictMode( 9730): at java.io.FileOutputStream.<init>(FileOutputStream.java:88)D/StrictMode( 9730): at com.example.strictmodedemo.MainActivity.writeToExternalStorage(MainActivity.java:56)D/StrictMode( 9730): at com.example.strictmodedemo.MainActivity.onCreate(MainActivity.java:30)D/StrictMode( 9730): at android.app.Activity.performCreate(Activity.java:4543)
因为上述属于主线程中的IO违例,解决方法就是讲写入操作放入工作线程。
1234567891011121314151617181920
public void writeToExternalStorage(){ new Thread(){ @Override
public void run(){ super.run();
File externalStorage= Environment.getExternalStorageDirectory();
File destFile= new File(externalStorage, "dest.txt");
try { OutputStream output= new FileOutputStream(destFile, true);
output.write("droidyue.com".getBytes());
output.flush();
output.close();
} catch (FileNotFoundException e){ e.printStackTrace();
} catch (IOException e){ e.printStackTrace();
}}}.start();
}
D/StrictMode(23890): StrictMode policy violation; ~duration=20 ms: android.os.StrictMode$StrictModeCustomViolation: policy=31 violation=8 msg=slowCall cost=2000
D/StrictMode(23890): at android.os.StrictMode$AndroidBlockGuardPolicy.onCustomSlowCall(StrictMode.java:1163)D/StrictMode(23890): at android.os.StrictMode.noteSlowCall(StrictMode.java:1974)D/StrictMode(23890): at com.example.strictmodedemo.TaskExecutor.executeTask(TaskExecutor.java:17)D/StrictMode(23890): at com.example.strictmodedemo.MainActivity.onCreate(MainActivity.java:36)D/StrictMode(23890): at android.app.Activity.performCreate(Activity.java:4543)D/StrictMode(23890): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)D/StrictMode(23890): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2158)D/StrictMode(23890): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2237)D/StrictMode(23890): at android.app.ActivityThread.access$600(ActivityThread.java:139)D/StrictMode(23890): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)D/StrictMode(23890): at android.os.Handler.dispatchMessage(Handler.java:99)D/StrictMode(23890): at android.os.Looper.loop(Looper.java:156)D/StrictMode(23890): at android.app.ActivityThread.main(ActivityThread.java:5005)D/StrictMode(23890): at java.lang.reflect.Method.invokeNative(Native Method)D/StrictMode(23890): at java.lang.reflect.Method.invoke(Method.java:511)D/StrictMode(23890): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)D/StrictMode(23890): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)D/StrictMode(23890): at dalvik.system.NativeStart.main(Native Method)
D/StrictMode( 2921): StrictMode policy violation; ~duration=31 ms: android.os.StrictMode$StrictModeDiskReadViolation: policy=31 violation=2
D/StrictMode( 2921): at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1176)D/StrictMode( 2921): at libcore.io.BlockGuardOs.read(BlockGuardOs.java:148)D/StrictMode( 2921): at libcore.io.IoBridge.read(IoBridge.java:422)D/StrictMode( 2921): at java.io.FileInputStream.read(FileInputStream.java:179)D/StrictMode( 2921): at java.io.InputStreamReader.read(InputStreamReader.java:244)D/StrictMode( 2921): at java.io.BufferedReader.fillBuf(BufferedReader.java:130)D/StrictMode( 2921): at java.io.BufferedReader.readLine(BufferedReader.java:354)D/StrictMode( 2921): at com.example.strictmodedemo.MainActivity.testReadContentOfFile(MainActivity.java:65)D/StrictMode( 2921): at com.example.strictmodedemo.MainActivity.onCreate(MainActivity.java:28)D/StrictMode( 2921): at android.app.Activity.performCreate(Activity.java:4543)