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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22903540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:32:39  来源:igfitidea点击:

Android - Copy files from assets to /data/data folder

androidandroid-fileandroid-assetsinternal-storage

提问by superuser

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 !

我需要在我的应用程序中使用一些文件。它们保存在资产文件夹中。我看到了关于 SO 的讨论,文件被从资产文件夹复制到内部存储上的 /data/data/,然后被使用。我得到了代码,但我没有得到的是,将资产复制到内部存储有什么需要?如果有人有这方面的经验,请帮忙!

回答by Markus Toman

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.

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

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 库,唯一现有的接口是一些“加载(字符*路径)”函数。

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

也许实际上有一些更好的方法,但我还没有找到。

回答by Lavekush Agrawal

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

回答by ali server

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

    }

回答by Rakki s

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.

我认为您无法在运行时或安装应用程序后编辑/修改资产文件夹中的数据。所以我们将文件移动到内部文件夹然后开始处理它。