android astesフォルダ内のファイルをsdカードに保存します。

2945 ワード

原文:http://mobile.51cto.com/aprogram-387591.htm
/**
     *     data 
     * @param ctxDealFile applicationContext
     * @param path
     */
    public void deepFile(Context ctxDealFile, String path) { 
        try { 
            String str[] = ctxDealFile.getAssets().list(path); 
            if (str.length > 0) {//      
                File file = new File(ctxDealFile.getFilesDir().getAbsolutePath()+"/" + path); 
                if (file.exists()) {

                }else {
                    file.mkdirs(); 
                    for (String string : str) { 
                        path = path + "/" + string; 
                        deepFile(ctxDealFile, path); 
                        path = path.substring(0, path.lastIndexOf('/')); 
                    } 
                }

            } else {//      
                InputStream is = ctxDealFile.getAssets().open(path); 
                FileOutputStream fos = new FileOutputStream(new File(ctxDealFile.getFilesDir().getAbsolutePath()+"/"  
                        + path)); 
                byte[] buffer = new byte[1024]; 
                int count = 0; 
                while (true) { 
                    count++; 
                    int len = is.read(buffer); 
                    if (len == -1) { 
                        break; 
                    } 
                    fos.write(buffer, 0, len); 
                } 
                is.close(); 
                fos.close(); 
            } 
        } catch (IOException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } 
    }