BitmapFactory.decodeStream始终返回null和Skia的解码器解码显示返回false
题
测试图像的位置: 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的类的错误。 你可以找到一个有效的解决方法,在这里的bug 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
尝试这种情况:
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);