【发布时间】:2012-04-17 02:29:07
【问题描述】:
所以我正在构建一个具有文件传输应用程序的应用程序,该应用程序允许您将文件从主文件夹中的一个文件夹传输到另一个文件夹。但是,我收到此错误“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)
【问题讨论】:
-
你是如何复制这些文件的?可以发一下代码吗?
-
源代码按要求添加。
-
GraphicsJNI 中的 logcat(它是 Android Java 代码和本机 Skia 库之间的粘合剂)看起来很像分配位图数据的问题。您能否发布完整的 logcat 堆栈,以便我们查看游览代码中的哪一行导致了问题?
-
Torid 是正确的,我使用的方法没有 try 和 catch 块,它从另一个 catch 块得到另一个错误,使它看起来像是内存的罪魁祸首。对不起大家。
标签: android memory file-transfer