Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add solution log for unhandled PNG file #6829

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -18,6 +18,7 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import javax.imageio.IIOException;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
Expand Down Expand Up @@ -59,29 +60,48 @@ static Point getImageSizeFromStream(InputStream is) {
}

static RobolectricBufferedImage getImageFromStream(InputStream is) {
return getImageFromStream(null, is);
}

static RobolectricBufferedImage getImageFromStream(String fileName, InputStream is) {
if (!initialized) {
// Stops ImageIO from creating temp files when reading images
// from input stream.
ImageIO.setUseCache(false);
initialized = true;
}

String format = null;
try {
ImageInputStream imageStream = createImageInputStream(is);
Iterator<ImageReader> readers = ImageIO.getImageReaders(imageStream);
if (!readers.hasNext()) return null;
if (!readers.hasNext()) {
return null;
}

ImageReader reader = readers.next();
try {
reader.setInput(imageStream);
String format = reader.getFormatName();
format = reader.getFormatName();
int minIndex = reader.getMinIndex();
BufferedImage image = reader.read(minIndex);
return RobolectricBufferedImage.create(image, ("image/" + format).toLowerCase());
} finally {
reader.dispose();
}
} catch (IOException e) {
Throwable cause = e.getCause();
if (FORMAT_NAME_PNG.equalsIgnoreCase(format)
&& cause instanceof IIOException
&& cause.getMessage() != null
&& cause.getMessage().contains("Invalid chunk length")) {
String pngFileName = "(" + (fileName == null ? "not given PNG file name" : fileName) + ")";
System.err.println(
utzcoz marked this conversation as resolved.
Show resolved Hide resolved
"The PNG file"
+ pngFileName
+ " cannot be decoded. This may be due to an OpenJDK issue with certain PNG files."
+ " See https://github.com/robolectric/robolectric/issues/6812 for more details.");
}
throw new RuntimeException(e);
}
}
Expand Down
Expand Up @@ -75,15 +75,16 @@ protected static Bitmap decodeResource(Resources res, int id, BitmapFactory.Opti
final TypedValue value = new TypedValue();
InputStream is = res.openRawResource(id, value);

RobolectricBufferedImage image = getImageFromStream(is);
String resourceName = res.getResourceName(id);
RobolectricBufferedImage image = getImageFromStream(resourceName, is);
if (!allowInvalidImageData && image == null) {
if (options != null) {
options.outWidth = -1;
options.outHeight = -1;
}
return null;
}
Bitmap bitmap = create("resource:" + res.getResourceName(id), options, image);
Bitmap bitmap = create("resource:" + resourceName, options, image);
ShadowBitmap shadowBitmap = Shadow.extract(bitmap);
shadowBitmap.createdFromResId = id;
return bitmap;
Expand All @@ -102,7 +103,7 @@ protected static Bitmap decodeFile(String pathName, BitmapFactory.Options option
if (pathName != null && new File(pathName).exists()) {
try (FileInputStream fileInputStream = new FileInputStream(pathName);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream)) {
image = getImageFromStream(bufferedInputStream);
image = getImageFromStream(pathName, bufferedInputStream);
} catch (IOException e) {
Logger.warn("Error getting size of bitmap file", e);
}
Expand Down