Frescopng fails to be loaded from the network for the first time due to internal criticism of Fresco.
As described in the question, this problem has existed in the project for a long time. Today, as my functions are on the homepage, the initial startup experience is extremely poor, so I have solved this problem.
Let's first describe how to experience a poor experience. That is, when I loaded the network PNG for the first time (I didn't know it was caused by this reason), I always showed the default image. According to Fresco's mechanism, first, the default image is displayed, and then the image is loaded on the network. When the image is downloaded, the original default image is replaced.
In fact, I have located an error before. I typed a log in the callback of the loaded image and found that all the images that were not loaded had gone through the onFailure method, throwing an exception.
IllegalArgumentException Problem decoding into existing bitmap
This exception is thrown in BitmapFactory. java. It is thrown in four places, namely different decodeResource, decodeByteArray, decodeStream, and decodeFileDescriptor. Let's take a look at one of the decodeStream methods.
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; } Bitmap bm = null; Trace.traceBegin(Trace.TRACE_TAG_GRAPHICS, "decodeBitmap"); try { if (is instanceof AssetManager.AssetInputStream) { final long asset = ((AssetManager.AssetInputStream) is).getNativeAsset(); bm = nativeDecodeAsset(asset, outPadding, opts); } else { bm = decodeStreamInternal(is, outPadding, opts); } if (bm == null && opts != null && opts.inBitmap != null) { throw new IllegalArgumentException("Problem decoding into existing bitmap"); } setDensityFromOptions(bm, opts); } finally { Trace.traceEnd(Trace.TRACE_TAG_GRAPHICS); } return bm; }
We can see that it is thrown out when bitmap is converted.
In combination with the article, we can conclude that Fresco has a BUG when loading PNG on the network, causing an exception when the stream is parsed to bitmap.
Solve the problem. Here I will talk about my solution.
The first solution is to modify the source code of Fresco in the link. You can go to line 1 of version 1.3.0. I will find it for you here.
public class DecodeProducer implements Producer
> { //...... @Override protected synchronized boolean updateDecodeJob(EncodedImage encodedImage, boolean isLast) { boolean ret = super.updateDecodeJob(encodedImage, isLast); if (!isLast && EncodedImage.isValid(encodedImage) && encodedImage.getImageFormat() == DefaultImageFormats.JPEG) { if (!mProgressiveJpegParser.parseMoreData(encodedImage)) { return false; } int scanNum = mProgressiveJpegParser.getBestScanNumber(); if (scanNum <= mLastScheduledScanNumber) { // We have already decoded this scan, no need to do so again return false; } if (scanNum < mProgressiveJpegConfig.getNextScanNumberToDecode(mLastScheduledScanNumber) && !mProgressiveJpegParser.isEndMarkerRead()) { // We have not reached the minimum scan set by the configuration and there // are still more scans to be read (the end marker is not reached) return false; } mLastScheduledScanNumber = scanNum; } return ret; } //......}
Add & encodedImage. getImageFormat () = DefaultImageFormats. PNG
if (scanNum < mProgressiveJpegConfig.getNextScanNumberToDecode(mLastScheduledScanNumber) && !mProgressiveJpegParser.isEndMarkerRead()) { // We have not reached the minimum scan set by the configuration and there // are still more scans to be read (the end marker is not reached) return false; }
As Fresco is already in version 1.8.0, you can locate this class and change it.
Solution 2
Use reflection to modify the method in the first solution. This does not change the source code of Fresco.
The third solution is the one I use.
Because the pictures in our App have seven cows, you only need to add them after your Url.
"?imageView2/1/format/jpg"
Convert png to jpg through qiniu.
However, it is impossible for the entire app to use jpg images, so one of the above two schemes is needed. If you have a better solution, please let me know. Long live open source.