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

时间:2021-12-18 00:27:13

I would like to copy a quite big directory from the assets folder of my app to the data folder on the first run of the app. How do I do that? I already tried some examples, but nothing worked, so I don't have anything. My target is Android 4.2.

我想在app的第一次运行时,把我app的assets文件夹里的一个大目录拷贝到data文件夹里,我该怎么做呢?我已经试过一些例子了,但是没有效果,所以我什么都没有。我的目标是Android 4.2。

Thanks, Yannik

谢谢,Yannik

1 个解决方案

#1


20  

try this code of your Application instance (you should write the class in manifest): This code is copying content of assets/files folder to the cache folder of app (you can place other path in copyAssetFolder() function). Only when App is launched for the first time

尝试应用程序实例的这段代码(您应该在manifest中编写类):这段代码将assets/files文件夹的内容复制到app的cache文件夹(您可以在copyAssetFolder()函数中放置其他路径)。只有当应用程序第一次启动时

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);
        }
    }

}

#1


20  

try this code of your Application instance (you should write the class in manifest): This code is copying content of assets/files folder to the cache folder of app (you can place other path in copyAssetFolder() function). Only when App is launched for the first time

尝试应用程序实例的这段代码(您应该在manifest中编写类):这段代码将assets/files文件夹的内容复制到app的cache文件夹(您可以在copyAssetFolder()函数中放置其他路径)。只有当应用程序第一次启动时

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);
        }
    }

}