Android file copy

Source: Internet
Author: User

Android file copy

Recently, a small test program was developed in the project, and files need to be copied. 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 RAW_DATA_CFG_FILE_NAME = "config.xml";

    // 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.RawDataTest"));            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, successfull!");                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 relatively simple, but a strange exception is reported in the middle, and it has been entangled for a long time. Fortunately, I found the root cause of the problem before leaving work. Write down this event, so that you can skip it.

The exception is as follows:

01-01 00:14:31.070: E/StrictMode(3297): A resource was 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.
 
  (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)
 
I also searched "java. io. closeable for information on avoiding resource leaks "and" java. lang. throwable: Explicit termination method 'close' not called ". I have not found any similar situations. Generally, it is caused by the memory leakage caused by cursor. Finally, I checked the code repeatedly and found that the root cause of the problem was not specified correctly in the target directory of the file I copied. When a problem occurs, I specify INTERNAL_STORAGE as "/mnt/sdcard0" and throw an exception when calling new FileOutputStream (destFile) because the directory does not exist. The root directory of the mobile phone's internal storage is "/mnt/sdcard", which actually points to "/storage/sdcard0 ".



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.