3

http 応答から画像を取得しようとしていますが、ストリームをビットマップに変換できません。ここで何が欠けているのか教えてください。

参考までに - 画像コンテンツは生のバイナリと jpeg 画像として受信されます。

手順は次のとおりです。

  1. HttpRequest を作成します。
  2. 応答で 200 を確認します -> httpentity コンテンツを取得します。
  3. BitMap factory を使用してストリームをビットマップに変換します。
  4. ビットマップをイメージビューに設定します

AsyncTask の postExecute でこれを行う

    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(endpoint);
    // Adding Headers .. 
    // Execute the request
    HttpResponse response;
    try {
        response = httpclient.execute(httpget);
    if (response.getStatusLine().getStatusCode() == 200) {
        // Get hold of the response entity
        HttpEntity entity = response.getEntity();
        if (entity != null) {
        InputStream instream = entity.getContent();
        return instream;
        // instream.close();
            }
    }
}

AsyncTask の postExecute でこれを行う

    if (null != instream) {
        Bitmap bm = BitmapFactory.decodeStream(instream);
        if(null == bm){
    Toast toast = Toast.makeText(getApplicationContext(),
        "Bitmap is NULL", Toast.LENGTH_SHORT);
            toast.show();
    }
        ImageView view = (ImageView) findViewById(R.id.picture_frame);
    view.setImageBitmap(bm);
    }

前もって感謝します。

4

2 に答える 2

5

最後にこれに対する答えを見つけました。以下はスニペットです - http 応答を扱う初心者に役立つかもしれません。

HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(endpoint);
// Adding Headers .. 
// Execute the request
HttpResponse response;
try {
    response = httpclient.execute(httpget);
if (response.getStatusLine().getStatusCode() == 200) {
    // Get hold of the response entity
    HttpEntity entity = response.getEntity();
    if (entity != null) {
    InputStream instream = entity.getContent();
    String path = "/storage/emulated/0/YOURAPPFOLDER/FILENAME.EXTENSION";
    FileOutputStream output = new FileOutputStream(path);
    int bufferSize = 1024;
    byte[] buffer = new byte[bufferSize];
    int len = 0;
    while ((len = instream.read(buffer)) != -1) {
        output.write(buffer, 0, len);
    }
    output.close();
}

ファイルをディスクに保存する代わりに、内容を bytearray に格納し、そこからビットマップを取得できます。

ByteArrayOutputStream baos = new ByteArrayOutputStream();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int len = 0;
try {
    // instream is content got from httpentity.getContent()
    while ((len = instream.read(buffer)) != -1) {
    baos.write(buffer, 0, len);
    }
    baos.close();
} catch (IOException e) {
    e.printStackTrace();
}
byte[] b = baos.toByteArray();
Bitmap bmp = BitmapFactory.decodeByteArray(b, 0, b.length);
ImageView imageView = (ImageView)findViewById(R.id.picture_frame);
imageView.setImageBitmap(bmp);

参考までに-アンドロイドのファイル出力ストリームでは、ローカルディスクへの書き込みは非UIスレッドで行う必要があります(私の場合は非同期タスクを使用しており、その部分はここに追加されていません)。

ありがとう ..

于 2014-02-24T09:54:29.013 に答える
1

このライブラリを使用して、Web からの画像を操作します。https://github.com/nostra13/Android-Universal-Image-Loader それはあなたのためにすべてを行います。ただし、onPostExecute のコードは onDoInBackground に配置する必要があります。onPre および onPost 実行コードはメイン スレッドで実行され、doInBackground はワーカー スレッドです。ただし、この場合はユニバーサルイメージローダーを使用してください

于 2014-02-20T21:29:52.887 に答える