将文件从资产复制到/data/data文件夹

时间:2022-07-09 00:28:11

I need to use some files in my app. They are kept in asset folder. I saw discussions on SO, where the files are being copied from asset folder, to /data/data/ on the internal storage, and then being used. I get the code, but what I do not get is, what is the need to copy the assets to internal storage ? If anybody has experience with this, help !

我需要在我的app中使用一些文件,它们保存在asset文件夹中。我看到了关于SO的讨论,其中文件被从asset文件夹复制到/data/data/内部存储,然后被使用。我得到了代码,但我没有得到的是,需要将资产复制到内部存储吗?如果有人对此有经验,请帮忙!

4 个解决方案

#1


8  

One reason that just popped up for me is when using existing C/C++ code with NDK that requires a path to a file and you don't want to modify that code.

我突然想到的一个原因是,使用带有NDK的现有C/ c++代码时,需要一个到文件的路径,而您不想修改该代码。

For example, I'm using an existing C library that needs some data files and the only existing interface is some "load( char* path )" function.

例如,我正在使用一个现有的C库,该库需要一些数据文件,惟一的现有接口是一些“load(char* path)”函数。

Perhaps there is actually some better method but I have not found any yet.

也许有更好的方法,但我还没有找到。

#2


6  

Try this:(Use all three method its work for me and assign destination path in "toPath" string object)

试试这个:(在“toPath”字符串对象中使用这三个方法及其工作并分配目标路径)

  String toPath = "/data/data/" + getPackageName();  // Your application path


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

#3


1  

public final String path = "/data/data/com.aliserver.shop/databases/";
public final String Name = "store_db";

public void _copydatabase() throws IOException {

        OutputStream myOutput = new FileOutputStream(path + Name);
        byte[] buffer = new byte[1024];
        int length;
        InputStream myInput = MyContext.getAssets().open("store_db");
        while ((length = myInput.read(buffer)) > 0) {
            myOutput.write(buffer, 0, length);
        }
        myInput.close();
        myOutput.flush();
        myOutput.close();

    }

#4


0  

I think you can't edit/modify data in asset folder in run time or after application installed .So we move files into internal folder then start working on it.

我认为你不能在运行时或应用安装后编辑/修改资产文件夹中的数据,所以我们将文件移到内部文件夹中,然后开始工作。

#1


8  

One reason that just popped up for me is when using existing C/C++ code with NDK that requires a path to a file and you don't want to modify that code.

我突然想到的一个原因是,使用带有NDK的现有C/ c++代码时,需要一个到文件的路径,而您不想修改该代码。

For example, I'm using an existing C library that needs some data files and the only existing interface is some "load( char* path )" function.

例如,我正在使用一个现有的C库,该库需要一些数据文件,惟一的现有接口是一些“load(char* path)”函数。

Perhaps there is actually some better method but I have not found any yet.

也许有更好的方法,但我还没有找到。

#2


6  

Try this:(Use all three method its work for me and assign destination path in "toPath" string object)

试试这个:(在“toPath”字符串对象中使用这三个方法及其工作并分配目标路径)

  String toPath = "/data/data/" + getPackageName();  // Your application path


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

#3


1  

public final String path = "/data/data/com.aliserver.shop/databases/";
public final String Name = "store_db";

public void _copydatabase() throws IOException {

        OutputStream myOutput = new FileOutputStream(path + Name);
        byte[] buffer = new byte[1024];
        int length;
        InputStream myInput = MyContext.getAssets().open("store_db");
        while ((length = myInput.read(buffer)) > 0) {
            myOutput.write(buffer, 0, length);
        }
        myInput.close();
        myOutput.flush();
        myOutput.close();

    }

#4


0  

I think you can't edit/modify data in asset folder in run time or after application installed .So we move files into internal folder then start working on it.

我认为你不能在运行时或应用安装后编辑/修改资产文件夹中的数据,所以我们将文件移到内部文件夹中,然后开始工作。