因此,我正在构建一个具有文件传输应用程序的应用程序,该应用程序允许您将文件从主文件夹内的一个文件夹传输到另一个文件夹。但是,我收到此错误“VM 不会让我们分配 ... 字节”。
Logcat 错误:
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
有没有人有办法克服这个问题?我看到了几篇关于图像而不是文件的帖子。
添加代码:(根据要求)。请注意,此代码也被复制到其他地方,但我自己做了一些修改。
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();
}
}
正如人们所提到的,内存错误是一个图像,是的,感谢您找到我令人尴尬的错误。完成这个 fileNotFoundException 后,我会尽快纠正它。
抱歉,长时间等待更新 logcat 忘记在复制文件方法中添加 try 和 catch,然后另一个方法从另一个方法捕获另一个错误,该方法显示顶部的内容:
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)