开发中遇到一个问题:
04-07 23:51:59.590: D/skia(7827): --- SkImageDecoder::Factory returned null
本来是下载一大批图片的,可是遇到了这个问题,图片没下载下来,
上网搜了一下,发现都是讲“是HttpURLConnection的问题,如果使用apache的HttpClient的话,就不会有问题了”
我一看,也以为是的,毕竟我也用了Java的HttpURLConnection,改了之后发现问题依然存在,恶心了一把。妹的,
只有一步步调试了,后来发现是图片压缩时的问题:BitmapFactory.decodeStream(is, null, opts);
就在这一步出了错,妹的,其他地方时这个地方就不会出错,怎么这个时候这地方出错了呢?
怎么这么恶心呢,,
明天继续调,,
现在是根据返回的bitmap去压缩图片,
code:
Bitmap bitmap = BitmapFactory.decodeStream(i, null, opt);
Matrix matrix = new Matrix();
matrix.postScale(0.5f,0.5f); //长和宽放大缩小的比例
Bitmap resizeBmp = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
return resizeBmp;
看这个错误的提示: SkImageDecoder::Factory returned null,想来是BitmapFactory的问题,找到decodeStream代码:
/**
* Decode an input stream into a bitmap. If the input stream is null, or
* cannot be used to decode a bitmap, the function returns null.
* The stream's position will be where ever it was after the encoded data
* was read.
*
* @param is The input stream that holds the raw data to be decoded into a
* bitmap.
* @param outPadding If not null, return the padding rect for the bitmap if
* it exists, otherwise set padding to [-1,-1,-1,-1]. If
* no bitmap is returned (null) then padding is
* unchanged.
* @param opts null-ok; Options that control downsampling and whether the
* image should be completely decoded, or just is size returned.
* @return The decoded bitmap, or null if the image data could not be
* decoded, or, if opts is non-null, if opts requested only the
* size be returned (in opts.outWidth and opts.outHeight)
*/
public static Bitmap decodeStream(InputStream is, Rect outPadding, Options opts) {
// we don't throw in this case, thus allowing the caller to only check
// the cache, and not force the image to be decoded.
if (is == null) {
return null;
}
// we need mark/reset to work properly
if (!is.markSupported()) {
is = new BufferedInputStream(is, DECODE_BUFFER_SIZE);
}
// so we can call reset() if a given codec gives up after reading up to
// this many bytes. FIXME: need to find out from the codecs what this
// value should be.
is.mark(1024);
Bitmap bm;
boolean finish = true;
if (is instanceof AssetManager.AssetInputStream) {
final int asset = ((AssetManager.AssetInputStream) is).getAssetInt();
if (opts == null || (opts.inScaled && opts.inBitmap == null)) {
float scale = 1.0f;
int targetDensity = 0;
if (opts != null) {
final int density = opts.inDensity;
targetDensity = opts.inTargetDensity;
if (density != 0 && targetDensity != 0) {
scale = targetDensity / (float) density;
}
}
bm = nativeDecodeAsset(asset, outPadding, opts, true, scale);
if (bm != null && targetDensity != 0) bm.setDensity(targetDensity);
finish = false;
} else {
bm = nativeDecodeAsset(asset, outPadding, opts);
}
} else {
// pass some temp storage down to the native code. 1024 is made up,
// but should be large enough to avoid too many small calls back
// into is.read(...) This number is not related to the value passed
// to mark(...) above.
byte [] tempStorage = null;
if (opts != null) tempStorage = opts.inTempStorage;
if (tempStorage == null) tempStorage = new byte[16 * 1024];
if (opts == null || (opts.inScaled && opts.inBitmap == null)) {
float scale = 1.0f;
int targetDensity = 0;
if (opts != null) {
final int density = opts.inDensity;
targetDensity = opts.inTargetDensity;
if (density != 0 && targetDensity != 0) {
scale = targetDensity / (float) density;
}
}
bm = nativeDecodeStream(is, tempStorage, outPadding, opts, true, scale);
if (bm != null && targetDensity != 0) bm.setDensity(targetDensity);
finish = false;
} else {
bm = nativeDecodeStream(is, tempStorage, outPadding, opts);
}
}
if (bm == null && opts != null && opts.inBitmap != null) {
throw new IllegalArgumentException("Problem decoding into existing bitmap");
}
return finish ? finishDecode(bm, outPadding, opts) : bm;
}
若图片数据不可被decoded,则返回空:“The decoded bitmap, or null if the image data could not be decoded”
应该是这个流不能被decoded,所以BitmapFactory.decodeSteam时返回null,但为什么不能被decode呢?
在这个BitmapFacotry中也没找到有关“SkImageDecoder::Factory returned null”的字符,在系统源码中找到文件:SkImageDecoder.cpp
其中:
bool SkImageDecoder::DecodeStream(SkStream* stream, SkBitmap* bm,
SkBitmap::Config pref, Mode mode, Format* format) {
SkASSERT(stream);
SkASSERT(bm);
bool success = false;
SkImageDecoder* codec = SkImageDecoder::Factory(stream);
if (NULL != codec) {
success = codec->decode(stream, bm, pref, mode);
if (success && format) {
*format = codec->getFormat();
}
delete codec;
}
return success;
}
在这里假如codec为空,则返回的success为false,然后系统哪里会根据这个返回值来提示那句提示呢?
先做项目,以后再解决这个问题。。