How to write a large file from an applications assets folder to the SD Card without getting an Out of Memory Error?

I am trying to copy a file of about 80 megabytes from the assets folder of an Android application to the SD card. The file is another apk. For various reasons I have to do it this way and can't simply link to an online apk or put it on the Android market. The application works fine with smaller apks but for this large one I am getting an out of memory error. I'm not sure exactly how this works but I am assuming that here I am trying to write the full 80 megabytes to memory. try { int length = 0; newFile.createNewFile(); InputStream inputStream = ctx.getAssets().open( "myBigFile.apk"); FileOutputStream fOutputStream = new FileOutputStream( newFile); byte[] buffer = new byte[inputStream.available()]; while ((length = inputStream.read(buffer)) > 0) { fOutputStream.write(buffer, 0, length); } fOutputStream.flush(); fOutputStream.close(); inputStream.close(); } catch (Exception ex) { if (ODP_App.getInstance().isInDebugMode()) Log.e(TAG, ex.toString()); } I found this interesting - [A question about an out of memory issue with Bitmaps][1] Unless I've misunderstood, in the case of Bitmaps, there appears to be some way to split the stream to reduce memory usage using BitmapFactory.Options. Is this do-able in my scenario or is there any other possible solution? [1]: https://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue
maybe your buffer is too big (trying to read the whole file into memory)? I am guessing that inputStream.available() gets the whole size? Change this to 1024 (or something)

以上就是How to write a large file from an applications assets folder to the SD Card without getting an Out of Memory Error?的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏