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/<package_name> 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?
Asked
Active
Viewed 3.4k times
14

desertnaut
- 57,590
- 26
- 140
- 166

superuser
- 625
- 3
- 7
- 19
-
1see [this](http://stackoverflow.com/questions/19218775/android-copy-assets-to-internal-storage) – user3355820 Apr 07 '14 at 04:21
-
1@user3355820 I get it how we can copy assets to internal storage or external storage. I get both are possible, eg, here is some sample code for copying file from asset to internal storage - http://stackoverflow.com/questions/15574983/copy-directory-from-assets-to-local-directory. What I don't get is, why would one want to do that. What is the significance of copying asset to internal/external storage. – superuser Apr 07 '14 at 04:26
4 Answers
9
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);
}
}

Lavekush Agrawal
- 6,040
- 7
- 52
- 85
-
3Right idea, but you should not hard code the /data directory (as it is subject to change) but rather discover it via the appropriate api at runtime. – Chris Stratton Jul 29 '14 at 14:41
-
Its depend on user , I was provide answer for copying asset folder to some path..and if you read question again user ask for "/data/data" clearly. – Lavekush Agrawal Jul 30 '14 at 04:15
-
@LavekushAgrawal this code will copy to `"/data/data/" + getPackageName()`... this is not exactly `/data/data/`. – ben75 Nov 22 '14 at 08:41
-
@ben75 Can you please edit my answer accordingly please, it will help to other ones. thank you. – Lavekush Agrawal Nov 22 '14 at 11:08
-
-
2Why not use the `
getFilesDir()` method instead of the "/data/data/ – Gensoukyou1337 Aug 21 '17 at 03:08"? -
I did this and got `permission denied` when trying to execute the binary asset – Daniel C Jacobs Mar 15 '22 at 19:20
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.
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.

Markus Toman
- 151
- 2
- 6
2
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();
}

ali server
- 29
- 1
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.

Rakki s
- 1,426
- 1
- 18
- 42