So I am building an app which have a File Transfer application which allows you to transfer files from one folder inside the main folder to another. However, I am getting this error “VM won’t let us allocate … bytes”.
Logcat Errors:
04-16 16:32:06.072: E/dalvikvm-heap(17644): 184440-byte external allocation too large for this process.
04-16 16:32:06.132: E/GraphicsJNI(17644): VM won't let us allocate 184440 bytes
Does anyone have a solution to overcome this? I saw several postings about images but not files.
Adding code: (As requested). Take note that this code is also copied elsewhere but there are a few modifications I made off my own.
private static void copyFile(File sourceFile, File destFile)
throws IOException {
if (!sourceFile.exists()) {
return;
}
if (!destFile.exists()) {
destFile.createNewFile();
}
destFile = new File(destFile.toString().substring(0, destFile.toString().lastIndexOf('/')));
FileChannel source = null;
FileChannel destination = null;
source = new FileInputStream(sourceFile).getChannel();
destination = new FileOutputStream(destFile).getChannel();
if (destination != null && source != null) {
destination.transferFrom(source, 0, source.size());
}
if (source != null) {
source.close();
}
if (destination != null) {
destination.close();
}
}
As people have mentioned the memmory error is an image and yes thank you for finding my emberassing bug. I will rectify it soon after I finished this fileNotFoundException.
Sorry for long wait on the logcat to be updated forgot to add in a try and catch inside the copy file method which another method then catches another error from another method which displayed what was on top:
04-16 18:19:41.322: W/System.err(4989): java.io.FileNotFoundException: /mnt/sdcard/ActivityApp (Is a directory)
04-16 18:19:41.322: W/System.err(4989): at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
04-16 18:19:41.322: W/System.err(4989): at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)
04-16 18:19:41.322: W/System.err(4989): at java.io.FileOutputStream.<init>(FileOutputStream.java:94)
04-16 18:19:41.322: W/System.err(4989): at java.io.FileOutputStream.<init>(FileOutputStream.java:66)
04-16 18:19:41.322: W/System.err(4989): at tab.layout.FileFusionActivity.copyFile(FileFusionActivity.java:107)
04-16 18:19:41.322: W/System.err(4989): at tab.layout.FileFusionActivity.onListItemClick(FileFusionActivity.java:78)
04-16 18:19:41.322: W/System.err(4989): at android.app.ListActivity$2.onItemClick(ListActivity.java:319)
04-16 18:19:41.322: W/System.err(4989): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
04-16 18:19:41.322: W/System.err(4989): at android.widget.ListView.performItemClick(ListView.java:3513)
04-16 18:19:41.322: W/System.err(4989): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
04-16 18:19:41.322: W/System.err(4989): at android.os.Handler.handleCallback(Handler.java:587)
04-16 18:19:41.322: W/System.err(4989): at android.os.Handler.dispatchMessage(Handler.java:92)
04-16 18:19:41.322: W/System.err(4989): at android.os.Looper.loop(Looper.java:123)
04-16 18:19:41.322: W/System.err(4989): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-16 18:19:41.322: W/System.err(4989): at java.lang.reflect.Method.invokeNative(Native Method)
04-16 18:19:41.322: W/System.err(4989): at java.lang.reflect.Method.invoke(Method.java:507)
04-16 18:19:41.322: W/System.err(4989): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-16 18:19:41.332: W/System.err(4989): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-16 18:19:41.332: W/System.err(4989): at dalvik.system.NativeStart.main(Native Method)
EDIT: i should have noticed it before but your log cat proves it. The error is in this line
change it to something like this
The error you are seeing might not be directly related to the copy process. Have you profiled you app to check the memory usage?
Another thing you can try is to copy the file in chunks as outlined in this question