Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Memory 从网站加载图像时如何减少内存?_Memory_Blackberry - Fatal编程技术网

Memory 从网站加载图像时如何减少内存?

Memory 从网站加载图像时如何减少内存?,memory,blackberry,Memory,Blackberry,我正在使用这个工具 public class Util_ImageLoader { public static Bitmap _bmap; Util_ImageLoader(String url) { HttpConnection connection = null; InputStream inputStream = null; EncodedImage bitmap; byte[] dataArray = null; try { c

我正在使用这个工具

public class Util_ImageLoader {
public static Bitmap _bmap;

Util_ImageLoader(String url) {
    HttpConnection connection = null;
    InputStream inputStream = null;
    EncodedImage bitmap;
    byte[] dataArray = null;

    try {
        connection = (HttpConnection) Connector.open(url + Util_GetInternet.getConnParam(), Connector.READ,
                true);
        inputStream = connection.openInputStream();
        byte[] responseData = new byte[10000];
        int length = 0;
        StringBuffer rawResponse = new StringBuffer();
        while (-1 != (length = inputStream.read(responseData))) {
            rawResponse.append(new String(responseData, 0, length));
        }
        int responseCode = connection.getResponseCode();
        if (responseCode != HttpConnection.HTTP_OK) {
            throw new IOException("HTTP response code: " + responseCode);
        }

        final String result = rawResponse.toString();
        dataArray = result.getBytes();
    } catch (final Exception ex) {
    }

    finally {
        try {
            inputStream.close();
            inputStream = null;
            connection.close();
            connection = null;
        } catch (Exception e) {
        }
    }

    bitmap = EncodedImage
            .createEncodedImage(dataArray, 0, dataArray.length);
    int multH;
    int multW;
    int currHeight = bitmap.getHeight();
    int currWidth = bitmap.getWidth();
    multH = Fixed32.div(Fixed32.toFP(currHeight), Fixed32.toFP(currHeight));// height
    multW = Fixed32.div(Fixed32.toFP(currWidth), Fixed32.toFP(currWidth));// width
    bitmap = bitmap.scaleImage32(multW, multH);

    _bmap = bitmap.getBitmap();
}

public Bitmap getbitmap() {
    return _bmap;
}
}
当我在一个包含10个child的
列表字段中调用它时,日志一直在说
未能分配计时器0:没有剩余的插槽


这意味着内存已用完,无法再次分配更多内存,因此我的主屏幕无法启动。

同时,内存中有以下对象:

    // A buffer of about 10KB
    byte[] responseData = new byte[10000];

    // A string buffer which will grow up to the total response size
    rawResponse.append(new String(responseData, 0, length));

    // Another string the same length that string buffer
    final String result = rawResponse.toString();

    // Now another buffer the same size of the response.        
    dataArray = result.getBytes();
如果您下载了n个ascii字符,那么总共有10KB,加上第一个unicode字符串缓冲区中的2*n字节,再加上
结果
字符串中的2*n字节,再加上
数据数组中的n字节。如果我没记错的话,总数是5n+10k。还有优化的余地

一些改进将是:

  • 首先检查响应代码,如果响应代码是HTTP 200,则读取流。如果服务器返回错误,则无需读取
  • 去掉绳子。如果在此之后再次转换为字节,则无需转换为字符串
  • 如果图像很大,下载时不要将其存储在RAM中。相反,打开一个
    FileOutputStream
    ,并在读取输入流时写入一个临时文件。然后,如果临时图像仍然大到可以显示,则缩小它们的比例

@Nate,我需要你的帮助。请看,它也使用了Util\u ImageLoader。在这个响应中,我提供了一个替代实现的完整代码,这应该会有所帮助。谢谢