The recent project has helped to make a small test program that requires copying files. The code is as follows:
Fixed for raw data test. private static final String raw_data_test = "***123***"; private static final String external_storage = "/mnt/sdcard2"; private static final String internal_storage = "/mnt/sdcard"; private static final String = "config. Raw_data_cfg_file_name";
Fixed for raw data test. Static public Boolean handlerawdatatest (context context, String input) {if (raw_data_test.equals (input)) { LOG.D (TAG, "handlerawdatatest"); if (!copyrawdatacfgfile ()) {LOG.D (TAG, "handlerawdatatest, get Raw Data Cfg file failed!"); Toast.maketext (Context, context.getstring (r.string.raw_data_cfg_file_not_exist), Toast.length_short). Show (); return false; } Intent Intent = new Intent (); Intent.setcomponent (New ComponentName ("Com.goodix.rawdata", "Com.goodix.rawdata.Ra Wdatatest ")); Context.startactivity (Intent); return true; } return false; public static Boolean Copyrawdatacfgfile () {String state = Environment.getexternalstoragestate (); if (! Environment.MEDIA_MOUNTED.equals (state)) { return false; } String srcfilename = external_storage + File.separator + raw_data_cfg_file_name; String destfilename = internal_storage + File.separator + raw_data_cfg_file_name; LOG.D (TAG, "copyrawdatacfgfile, Srcfilename =" + Srcfilename + ", destFileName =" + destFileName); try {if (CopyFile (Srcfilename, destFileName, True)) {LOG.D (TAG, "Copyrawdatacfgfile, Successfu Ll! "); return true; }} catch (Exception e) {e.printstacktrace (); } log.d (TAG, "Copyrawdatacfgfile, fail!"); return false; public static Boolean CopyFile (String srcfilename, String destFileName, Boolean reWrite) throws IOException { LOG.D (TAG, "copyFile, Begin"); File Srcfile = new file (srcfilename); File DestFile = new file (destfilename); if (!srcfile.exists ()) {LOG.D (TAG, "copyFile, source file not exist."); Return False } if (!srcfile.isfile ()) {LOG.D (TAG, "copyFile, source file not a file."); return false; } if (!srcfile.canread ()) {LOG.D (TAG, "copyFile, source file can ' t read."); return false; } if (Destfile.exists () && reWrite) {log.d (TAG, "copyFile, before copy File, delete first."); Destfile.delete (); } try {InputStream instream = new FileInputStream (srcfile); FileOutputStream OutStream = new FileOutputStream (destfile); byte[] buf = new byte[1024]; int byteread = 0; while ((Byteread = Instream.read (BUF))! =-1) {outstream.write (buf, 0, Byteread); } outstream.flush (); Outstream.close (); Instream.close (); } catch (IOException e) {throw e; } catch (Exception e) {e.printstacktrace (); } log.d (TAG, "CopYfile, Success "); return true; }
The code is simple, but the middle reported a very strange anomaly, tangled for a long time, fortunately, before work to find the root of the problem. Make a note of the incident.
Exceptions are as follows:
01-01 00:14:31.070:e/strictmode (3297): A resource is acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.01-01 00:14:31.070:e/strictmode (3297): Java.lang.Throwable:Explicit termination method ' close ' not called01-01 00:14:31.070:e/strictmode (3297): at Dalvik.system.CloseGuard.open (closeguard.java:184) 01-01 00:14:31.070:e/strictmode (3297): at Java.io.fileinputstream.<init> (fileinputstream.java:80) 01-01 00:14:31.070:e/strictmode (3297): at Com.android.dialer.SpecialCharSequenceMgr.copyFile (specialcharsequencemgrproxy.java:768) 01-01 00:14:31.070:e/ Strictmode (3297): at Com.android.dialer.SpecialCharSequenceMgr.copyRawDataCfgFile ( specialcharsequencemgrproxy.java:702) 01-01 00:14:31.070:e/strictmode (3297): at Com.android.dialer.SpecialCharSequenceMgr.handleRawDataTest (specialcharsequencemgrproxy.java:674) 01-01 00:14:31.070:e/strictmode (3297): at Com.android.dialer.SpecialCharSequenceMgr.handleCharsForTest (specialcharsequencemgrproxy.java:618) 01-01 00:14:31.070:e/strictmode (3297): at Com.android.dialer.SpecialCharSequenceMgr.handleChars (specialcharsequencemgrproxy.java:88) 01-01 00:14:31.070:e/ Strictmode (3297): at Com.android.dialer.dialpad.DialpadFragment.afterTextChanged (dialpadfragment.java:451) 01-01 00:14:31.070:e/strictmode (3297): at Android.widget.TextView.sendAfterTextChanged (textview.java:7626) 01-01 00:14:31.070:e/strictmode (3297): at Android.widget.textview$changewatcher.aftertextchanged (TextView.java:9424)
"Java.io.Closeable for information on avoiding resource leaks" and "java.lang.Throwable:Explicit termination method" were also searched online Close ' not called ', none of which is similar to my problem, usually a memory leak caused by the cursor. Eventually, the code was repeatedly reviewed to find that the root of the problem was not specified correctly in the destination directory where I copied the file. In the event of a problem, I specified internal_storage as "/mnt/sdcard0", which throws an exception when calling to New FileOutputStream (DestFile), because there is no such directory. The internal storage root directory of the phone is "/mnt/sdcard", which actually points to "/storage/sdcard0".
Android file copy