BitmapFactory.decodeStreamは常にnullを返しますとskiaデコーダを示してデコードfalseを返した

StackOverflow https://stackoverflow.com/questions/3802820

  •  25-09-2019
  •  | 
  •  

質問

テスト画像はこちらhttp://images.plurk.com/tn_4134189_bf54fe8e270ce41240d534b5133884ee.gif

されたソリューションにつは、インターネットでの送り不良の原因となります。

私が使っている以下のスニペットコード:

Url imageUrl = new Url("http://images.plurk.com/tn_4134189_bf54fe8e270ce41240d534b5133884ee.gif");
Bitmap image = BitmapFactory.decodeStream(imageUrl.openStream());

常にこのログには:

DEBUG/skia(1441): --- decoder->decode returned false

さい感謝。

編集:

このように映像が復号化されているものでないことを示し、 WebView.ができる場で開きます。

役に立ちましたか?

解決

うとして一時的回避策:

最初に以下のエントリを追加すクラス:

  public static class PlurkInputStream extends FilterInputStream {

    protected PlurkInputStream(InputStream in) {
        super(in);
    }

    @Override
    public int read(byte[] buffer, int offset, int count)
        throws IOException {
        int ret = super.read(buffer, offset, count);
        for ( int i = 2; i < buffer.length; i++ ) {
            if ( buffer[i - 2] == 0x2c && buffer[i - 1] == 0x05
                && buffer[i] == 0 ) {
                buffer[i - 1] = 0;
            }
        }
        return ret;
    }

}

プを通しオリジナルのストリームPlurkInputStream:

Bitmap bitmap = BitmapFactory.decodeStream(new PlurkInputStream(originalInputStream));

私の場合このお手伝いさせていただきます。

編集:

いてみてください以下のバージョンではなく:

        for ( int i = 6; i < buffer.length - 4; i++ ) {
            if ( buffer[i] == 0x2c ) {
                if ( buffer[i + 2] == 0 && buffer[i + 1] > 0
                    && buffer[i + 1] <= 48 ) {
                    buffer[i + 1] = 0;
                }
                if ( buffer[i + 4] == 0 && buffer[i + 3] > 0
                    && buffer[i + 3] <= 48 ) {
                    buffer[i + 3] = 0;
                }
            }
        }

これは有効でないコードもこのアルバイト、正しい解決策です。では、ほとんどの場合ますが、全てではありません。

他のヒント

私は同じ問題を抱えていた、部分的にこのクラスで修正されました。

static class FlushedInputStream extends FilterInputStream {
public FlushedInputStream(InputStream inputStream) {
    super(inputStream);
}

@Override
public long skip(long n) throws IOException {
    long totalBytesSkipped = 0L;
    while (totalBytesSkipped < n) {
        long bytesSkipped = in.skip(n - totalBytesSkipped);
        if (bytesSkipped == 0L) {
              int byte = read();
              if (byte < 0) {
                  break;  // we reached EOF
              } else {
                  bytesSkipped = 1; // we read one byte
              }
       }
        totalBytesSkipped += bytesSkipped;
    }
    return totalBytesSkipped;
}

}

InputStream in = null;
    try {
        in = new java.net.URL(imageUrl).openStream();
        } catch (MalformedURLException e) {
        e.printStackTrace();
        } catch (IOException e) {
        e.printStackTrace();
        }
Bitmap image = BitmapFactory.decodeStream(new FlushedInputStream(in));

これは、ほとんどのケースで助けたが、これは普遍的解決策ではありません。 もっとこのバグ報告するます。

を参照してください。

ベスト運!

私はすべてのソリューションを試みたが、私の問題を解決していませんでした。いくつかのテストの後、失敗デコーダskiaの問題は、インターネット接続が安定していない多くのことを起こりました。私にとっては、画像を再ダウンロードするために強制的に問題を解決します。

画像は大きいサイズである場合、

は問題がより提示されます。

ループを使用するには、ほとんどの2回の再試行で私を必要とし、イメージが正しくダウンロードされます。

Bitmap bmp = null;
int retries = 0;
while(bmp == null){
    if (retries == 2){
        break;
    }
    bmp = GetBmpFromURL(String imageURL);
    Log.d(TAG,"Retry...");
    retries++;
}

これは動作するはずます:

URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
connection.disconnect();
input.close();

myBitmapあなたのイメージが含まれています。

これはAndroidの中のInputStreamクラスのバグによるものです。 あなたは、有効な回避策と、ここでバグ HTTPの説明を見つけることができます:// code.google.com/p/android/issues/detail?id=6066する

は、メモリ上の理由から、あなたはこのような実装BitmapFactoryオプションでなければなりません

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4; // might try 8 also

多分このような主要なダウンロードビットマップ機能:

Bitmap downloadBitmap(String url) {

    final HttpClient client = AndroidHttpClient.newInstance("Android");
    final HttpGet getRequest = new HttpGet(url);

    try {
        HttpResponse response = client.execute(getRequest);
        final int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            if(DEBUG)Log.w("ImageDownloader", "Error " + statusCode +
                    " while retrieving bitmap from " + url);
            return null;
        }

        final HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream inputStream = null;
            try {

                inputStream = entity.getContent();
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 4; // might try 8 also
                return BitmapFactory.decodeStream(new FlushedInputStream(inputStream),null,options);

            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }
                entity.consumeContent();
            }
        }
    } catch (IOException e) {
        getRequest.abort();
        if(DEBUG)Log.w(TAG, "I/O error while retrieving bitmap from " + url, e);
    } catch (IllegalStateException e) {
        getRequest.abort();
        if(DEBUG)Log.w(TAG, "Incorrect URL: " + url);
    } catch (Exception e) {
        getRequest.abort();
        if(DEBUG)Log.w(TAG, "Error while retrieving bitmap from " + url, e);
    } finally {
        if ((client instanceof AndroidHttpClient)) {
            ((AndroidHttpClient) client).close();
        }
    }
    return null;
}

そして多分あなたは、このような実装AsyncTaskでなければなりません。 http://android-developers.blogspot.com/2010/07 /multithreading-for-performance.htmlする

私にとっての問題は、画像の色の種類である:あなたの画像がカラーである= CYMKないRGBでの

多分これはあなたのケースではありませんが、あなたが代わりにRGB色空間の、CMYK色空間で画像を解読しようとしている場合、それは可能性があります。 CMYK画像は、この1 のように、アンドロイドでサポートされていない、とさえで表示されませんAndroidのWebブラウザ。続きを読むこのにここにhref="https://stackoverflow.com/questions/11047224/unable-to-load-jpeg-image-with-bitmapfactory-decodefile-returns-null">:

BitmapFactoryでJPEGイメージをロードする

できません。 decodeFile。戻り値はnull

これを試してみてください

HttpGet httpRequest = new HttpGet(url);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufferedHttpEntity = new BufferedHttpEntity(entity);
InputStream is = bufferedHttpEntity.getContent();
Drawable d = Drawable.createFromStream(is, "");
//or bitmap
//Bitmap b = BitmapFactory.decodeStream(is);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top