Copying large file from asset folder

628 views
Skip to first unread message

kaushik mv

unread,
Jun 23, 2011, 1:22:50 AM6/23/11
to android-ndk
Hi,

I have problem in copying file from assets folder to /data/data/
<app_name> folder. I am using the following code to copy the files .
private void CopyAssets() {

AssetManager assetManager = getAssets();
String[] files = null;
try {
files = assetManager.list("");
} catch (IOException e) {
Log.e("tag", e.getMessage());
}
for(int i=0; i<files.length; i++) {
InputStream in = null;
OutputStream out = null;

try {

in = assetManager.open(files[i]);
PackageManager pm = getPackageManager();
String dataDir =
pm.getApplicationInfo(getPackageName(),
0).dataDir;
String S =dataDir+"/";

out = new FileOutputStream(S+ files[i]);

copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch(Exception e) {

Log.e("tag", e.getMessage());
}
}
}
private 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);
}
}

While the above code works for small files, it is not copying large
file(1.2 MB). Please help me to debug this.

thanks in advance,
kaushik.

Dianne Hackborn

unread,
Jun 23, 2011, 5:16:29 PM6/23/11
to andro...@googlegroups.com
Prior to Android 2.3, you could not open compressed files that were larger than 1MB.  You will need to store it uncompressed in the .apk.  (You could still have the data compressed yourself, and decompress it when you copy the stream.  This is for example how PNG images are effectively stored.  They are compressed by the PNG format and that data within the .apk is thus not compressed (again)).


--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.




--
Dianne Hackborn
Android framework engineer
hac...@android.com

Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails.  All such questions should be posted on public forums, where I and others can see and answer them.

Tim Mensch

unread,
Jun 24, 2011, 4:05:56 PM6/24/11
to andro...@googlegroups.com
On 6/23/2011 3:16 PM, Dianne Hackborn wrote:
> Prior to Android 2.3, you could not open compressed files that were
> larger than 1MB. You will need to store it uncompressed in the .apk.
> (You could still have the data compressed yourself, and decompress it
> when you copy the stream. This is for example how PNG images are
> effectively stored. They are compressed by the PNG format and that
> data within the .apk is thus not compressed (again)).

I ran into this myself when I accidentally "compressed" an OGG file in
an APK I was rebuilding, which Android promptly failed to open. I worked
around it by compressing the APK (using 7-zip) in two steps -- once with
compression turned on, and once with compression set to "store."

I was rebuilding the APK because I'm doing some ... creative? ... things
to prevent the Google Licensing Service from being trivially broken.
Anything can be hacked, of course, but it didn't seem worthwhile to even
use the Licensing Service if there's a script out in the wild that
automatically cracks it. :(

I hope that it will at least postpone someone hacking my new game until
it gets some momentum; it will certainly prevent an average user from
simply using the cracking script. We'll see what happens. :)

Tim

Reply all
Reply to author
Forward
0 new messages