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

2020-02-09 08:54发布

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 !

4条回答
SAY GOODBYE
2楼-- · 2020-02-09 09:31
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();

    }
查看更多
一纸荒年 Trace。
3楼-- · 2020-02-09 09:34

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.

查看更多
狗以群分
4楼-- · 2020-02-09 09:35

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

  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);
        }
    }
查看更多
太酷不给撩
5楼-- · 2020-02-09 09:52

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.

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.

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

查看更多
登录 后发表回答