0

このユーティリティを使用しています

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 個の子を含むで呼び出すとlistfield、ログは と言い続けますfailed to allocate timer 0: no slots left

これは、メモリが使い果たされており、再度割り当てるメモリがないことを意味し、その結果、メイン画面を起動できません。

4

1 に答える 1

2

同時に、メモリには次のオブジェクトがあります。

    // 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 バイト、さらにresult文字列に 2*n バイト、および に n バイトが含まれdataArrayます。私が間違っていなければ、合計すると 5n + 10k になります。最適化の余地があります。

いくつかの改善点は次のとおりです。

  • 最初に応答コードを確認し、応答コードが HTTP 200 の場合はストリームを読み取ります。サーバーがエラーを返した場合は読み取る必要はありません。
  • 文字列を取り除きます。その後、再度バイトに変換する場合は、文字列に変換する必要はありません。
  • 画像が大きい場合は、ダウンロード中に RAM に保存しないでください。代わりに、FileOutputStream入力ストリームから読み取るときに、a を開いて一時ファイルに書き込みます。次に、一時的なイメージがまだ表示するのに十分な大きさである場合は、それらを縮小します。
于 2012-07-12T08:25:11.960 に答える