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

Java 将目录从资产复制到数据文件夹,java,android,eclipse,android-4.2-jelly-bean,Java,Android,Eclipse,Android 4.2 Jelly Bean,我想复制一个相当大的目录,从我的应用程序的资产文件夹到应用程序第一次运行时的数据文件夹。 我该怎么做?我已经试过一些例子了,但是没有效果,所以我什么都没有。我的目标是安卓4.2 谢谢, Yannik尝试应用程序实例的以下代码(您应该在清单中编写类): 此代码正在将资产/文件文件夹的内容复制到应用程序的缓存文件夹中(您可以在copyAssetFolder()函数中放置其他路径)。仅当应用程序首次启动时 import java.io.File; import java.io.FileOutputSt

我想复制一个相当大的目录,从我的应用程序的资产文件夹到应用程序第一次运行时的数据文件夹。 我该怎么做?我已经试过一些例子了,但是没有效果,所以我什么都没有。我的目标是安卓4.2

谢谢,
Yannik

尝试应用程序实例的以下代码(您应该在清单中编写类): 此代码正在将资产/文件文件夹的内容复制到应用程序的缓存文件夹中(您可以在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);
        }
    }

}

你为什么要那样做?如果我没有弄错的话,data and asset文件夹将在应用程序安装后一直有效。请检查此选项,这可能对你们有用,谢谢,但我想将其放在data文件夹中,因为我想在文件夹中运行脚本。你指的是哪个文件夹?你能写这个的绝对路径吗?例如/data/data/com.example.appname/据我所知,你不能这样做,但你可以使用文件夹/data/data/com.example.appname/files-请参阅更新的codelet