首页 文章

将目录从资产复制到数据文件夹

提问于
浏览
9

我想将一个非常大的目录从我的应用程序的assets文件夹复制到第一次运行应用程序的数据文件夹 . 我怎么做?我已经尝试了一些例子,但没有任何效果,所以我没有任何东西 . 我的目标是Android 4.2 .

谢谢,Yannik

1 回答

  • 21

    尝试使用Application实例的这段代码(你应该在manifest中编写类):这段代码将assets / files文件夹的内容复制到app的缓存文件夹中(你可以在copyAssetFolder()函数中放置其他路径) . 仅在App首次启动时

    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    
    import android.app.Application;
    import android.content.Context;
    import android.content.res.AssetManager;
    import android.preference.PreferenceManager;
    
    public class MyApplication extends Application {
        private static Context  s_sharedContext;
    
        @Override
        public void onCreate () {
            super.onCreate();   
            if (!PreferenceManager.getDefaultSharedPreferences(
                    getApplicationContext())
                .getBoolean("installed", false)) {
                PreferenceManager.getDefaultSharedPreferences(
                        getApplicationContext())
                    .edit().putBoolean("installed", true).commit();
    
                copyAssetFolder(getAssets(), "files", 
                        "/data/data/com.example.appname/files");
            }
        }
    
        private static boolean copyAssetFolder(AssetManager assetManager,
                String fromAssetPath, String toPath) {
            try {
                String[] files = assetManager.list(fromAssetPath);
                new File(toPath).mkdirs();
                boolean res = true;
                for (String file : files)
                    if (file.contains("."))
                        res &= copyAsset(assetManager, 
                                fromAssetPath + "/" + file,
                                toPath + "/" + file);
                    else 
                        res &= copyAssetFolder(assetManager, 
                                fromAssetPath + "/" + file,
                                toPath + "/" + file);
                return res;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
    
        private static boolean copyAsset(AssetManager assetManager,
                String fromAssetPath, String toPath) {
            InputStream in = null;
            OutputStream out = null;
            try {
              in = assetManager.open(fromAssetPath);
              new File(toPath).createNewFile();
              out = new FileOutputStream(toPath);
              copyFile(in, out);
              in.close();
              in = null;
              out.flush();
              out.close();
              out = null;
              return true;
            } catch(Exception e) {
                e.printStackTrace();
                return false;
            }
        }
    
        private static void copyFile(InputStream in, OutputStream out) throws IOException {
            byte[] buffer = new byte[1024];
            int read;
            while((read = in.read(buffer)) != -1){
              out.write(buffer, 0, read);
            }
        }
    
    }
    

相关问题