【问题标题】:Android - Copy files from assets to /data/data folderAndroid - 将文件从资产复制到 /data/data 文件夹
【发布时间】:2014-04-07 04:19:28
【问题描述】:

我需要在我的应用程序中使用一些文件。它们保存在资产文件夹中。我看到了关于 SO 的讨论,文件从资产文件夹复制到内部存储上的 /data/data/,然后被使用。 我得到了代码,但我没有得到的是,需要将资产复制到内部存储吗?

【问题讨论】:

  • this
  • @user3355820 我知道我们如何将资产复制到内部存储或外部存储。我得到两者都是可能的,例如,这是一些用于将文件从资产复制到内部存储的示例代码 - stackoverflow.com/questions/15574983/…。我不明白的是,为什么要这样做。将资产复制到内部/外部存储有什么意义。

标签: android android-file android-assets internal-storage


【解决方案1】:

试试这个:(使用它对我的工作的所有三种方法,并在“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);
        }
    }

【讨论】:

  • 正确的想法,但您不应该对 /data 目录进行硬编码(因为它可能会发生变化),而是在运行时通过适当的 api 发现它。
  • 它取决于用户,我提供了将资产文件夹复制到某个路径的答案。如果您再次阅读问题,用户会清楚地询问“/data/data”。
  • @LavekushAgrawal 此代码将复制到"/data/data/" + getPackageName()...这不完全是/data/data/
  • @ben75 您能否相应地编辑我的答案,这将对其他人有所帮助。谢谢。
  • 为什么不使用<application Context>getFilesDir() 方法而不是“/data/data/”?
【解决方案2】:

我刚刚弹出的一个原因是,当使用现有的 C/C++ 代码和 NDK 时,需要文件路径,而您不想修改该代码。

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

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

【讨论】:

    【解决方案3】:
    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】:

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

      【讨论】:

        猜你喜欢
        • 2015-01-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-14
        • 2011-08-07
        • 1970-01-01
        • 2011-07-12
        • 1970-01-01
        相关资源
        最近更新 更多