此处测试图片: 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
上显示。但是可以看看是否在浏览器中打开。
答案 0 :(得分:4)
我有同样的问题,部分是由这个类修复的:
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));
在大多数情况下它有所帮助,但这不是通用解决方案。 有关更多信息,请参阅此bugreport。
祝你好运!
答案 1 :(得分:4)
尝试将其作为临时解决方法:
首先添加以下类:
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;
}
}
}
请注意,这不是有效的代码,也不是完整/正确的解决方案。它适用于大多数情况,但不是全部。
答案 2 :(得分:1)
我尝试了所有的解决方案,但没有解决我的问题。经过一些测试,当互联网连接不稳定时,skia解码器失败的问题发生了很多。对我来说,强制重新下载图像解决了这个问题。
当图像尺寸较大时,问题也会更多。
使用循环将需要我最多2次重试,并且图像将被正确下载。
Bitmap bmp = null;
int retries = 0;
while(bmp == null){
if (retries == 2){
break;
}
bmp = GetBmpFromURL(String imageURL);
Log.d(TAG,"Retry...");
retries++;
}
答案 3 :(得分:0)
这应该有效:
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包含您的图片。
答案 4 :(得分:0)
这是由于Android中的InputStream类中的错误。 您可以在http://code.google.com/p/android/issues/detail?id=6066
找到有效的解决方法和错误说明答案 5 :(得分:0)
试试这个:
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);
答案 6 :(得分:0)
出于内存原因,您必须实现这样的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
答案 7 :(得分:0)
对我来说,问题在于图像的颜色类型:您的图像是彩色= CYMK不是RGB
答案 8 :(得分:0)
也许这不是你的情况,但可能是你试图用CMYK颜色空间而不是RGB颜色空间解码图像。 Android不支持CMYK图像,如this one,即使在Android网络浏览器中也不会显示。详细了解此here:
Unable to load JPEG-image with BitmapFactory.decodeFile. Returns null