装载的OutOfMemoryError在Android中大量数据时, [英] OutOfMemoryError when loading large amount of data in Android

查看:168
本文介绍了装载的OutOfMemoryError在Android中大量数据时,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下载的Andr​​oid设备(60MB〜)的SD卡上的一个相当大的文件。下载是很好,但我需要拉该文件的全部内存。我知道这是内存对于一款智能手机一个巨大的吸盘,但我知道我的设备已经超过一倍金额。不管怎样,我实际的问题是,我得到一个文件读入一个字节数组时,的OutOfMemoryError 。有什么办法来提高我的应用程序分配的内存,或者某种方式假参考文件还保存在外部存储文件时不加载到内存中?

I'm downloading a rather large file on the SD card of an Android device (~60MB). The download goes fine, but I need to pull this file in its entirety into memory. I know that's a huge chuck of memory for a smart phone, but I know my device has more than double that amount. Anyway, my actual problem is I get a OutOfMemoryError when reading the file into a byte array. Is there any way to increase the allocated memory for my application, or some way to "fake" reference the file yet keep the file in external storage without loading it into internal memory?

推荐答案

在Android的每个虚拟机实例都有他们所谓的虚拟机的预算。这基本上是内存的整个应用程序可以占用而已最大金额。 getRuntime()。maxMemory()会给你的虚拟机的预算规模为你的当前实例。

Each Virtual Machine instance in Android has what they called a VM Budget. This basically is the max amount of memory the entire app can occupy and nothing more. getRuntime().maxMemory() will give you the vm budget size for your current instance.

预算可以从16MB(G1)的任何地方去48MB(MOTO Xoom的),我认为。这些数字可能是因为我想记住我的头顶部的不同。

The budget can go anywhere from 16MB (G1) to 48MB (Moto Xoom) I think. Those numbers could be different as I'm trying to remember off the top of my head.

通常这样的事情你会在作为InputStream读取数据,并为你读它,但不保留数据本身处理数据。

Usually for something like this you'd read the data in as an InputStream and process the data as you read it but don't retain the data itself.

如果这是一个位图。您可以使用...

If this is a Bitmap. You can use...

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4; // Divides the width and height each by 4 when reading the data in.
Bitmap bitmap = BitmapFactory.decodeFile(String "filePath", options);
Bitmap bitmap = BitmapFactory.decodeStream(InputStream "fileStream", options);

这篇关于装载的OutOfMemoryError在Android中大量数据时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆