9-patch image error in Android
Asked Answered
P

14

58

Whenever I try to add a 9-patch image to the \drawable folder in my project, I get the same error:

ERROR: 9-patch image C:\...\res\drawable\appwidget.9.png malformed.
Frame pixels must be either solid or transparent (not intermediate alphas).
Found at pixel #3 along top edge. Failure processing PNG image
C:\...\res\drawable\appwidget.9.png

The weird thing in this example is that I've got this image from the "android-g1-icon" images found in the SDK resources, so it is supposed to be OK. The same happened with any 9-patch image I've tried so far. What could be wrong?

Puffy answered 25/11, 2010 at 19:43 Comment(1)
have you tried it with your own 9.png? When you define patches, make sure to define exactly one along each side of the imageThermocouple
I
32

I have encountered with same problem on Android Studio:

AAPT out(943142208) : No Delegate set : lost message:Done
AAPT err(943142208): ERROR: 9-patch image /Users/cartman/Github/UteacherAndroid/RefactorDemo/app/src/main/res/drawable-xxhdpi/nav_shabow.9.png malformed.
AAPT err(943142208):        Frame pixels must be either solid or transparent (not intermediate alphas).
AAPT err(943142208):        Found at pixel #1 along left edge.

This is how I resolved it: Open draw9patch tool under your Android Sdk directory

cd /Android/sdk/tools
./draw9patch

Open .9 png and save it again. Hope it helps.

Isomorphism answered 3/8, 2015 at 6:21 Comment(2)
Thank you! If you are using Android Studio just open this image and save (Android Studio automatically use draw9patch tool).Cecilacecile
i just open draw9patch image through folder Android>sdk>tool double click the drw9patch . Then a cmd and 9 patch image is open in a new window, after that what should i do @Jiawei DaiHollow
I
23

This is the problem with the latest ADT that is 20.0.3. You can instead rename the *.9.png to *.png and start working.

I think this is the bug with the ADT only, since for 18.0.0 version ADT it doesn't prompt for this type of error and works fine.

Illume answered 24/8, 2012 at 9:20 Comment(8)
This looks like a workaround. But This has fixed my problem though. ThanksJennettejenni
Will renaming these .9.png to .png image cause any difference my apps behavior. I mean will these renamed .png images stretch same way as .9.pngs ?Molybdous
using these you would be just able to run the application in device or emulator without getting error. from the ui side what difference it will make i have no idea about it.Illume
removing the .9 from the file name allows the app to functions but is not a workaround because it removes the stretched regions efficively making it the same as the original file.Chumash
yes. it is workaround only to get things things worked temporarily.Illume
@Android_Rocks - In UI it gets stretchedPaulettepauley
@Vivo: it might be due the image not in proper formatIllume
@Android_Rocks, then how you can stretch image without 9 patch? Any alternative?Miki
S
11

The resource compiler strips 9-patches one-pixel borders away (and presumably stores the information gained from them in a more efficient way outside of the .9.png file). If you've fished these .9.png files out of an .apk, you'll need to add the border back by hand.

Smallish answered 25/11, 2010 at 19:53 Comment(5)
Hmm, that makes sense. It's weird that the images in sdk resources are already optimized, but I'll try to fix them or create one by hand (although my artistic abilities are null). Many thanks!Puffy
I took some .9.png images from Google_Keyboard.apk and getting same issue. Could you please explain in detail how can add border myself?Molybdous
@ShaktiMalik use a 9-patch editor to define the patches and padding for the files -- either the one built into Android Studio or the one in the sdk/tools folder.Quevedo
How does AAPT store a 9-patch metainformation? I have tried to assemble APK (just in Android studio), take a crunched 9-patch (from the compiled APK), recompress it in GIMP (with saving comments), and, then, patch the APK with modified 9-patch. But, it seems it's not stretching it anymore in a correct manner. What can be wrong and how does AAPT process 9-patch PNGs?Sarnoff
Use the source... android.googlesource.com/platform/frameworks/base/+/…Smallish
F
8

I had the same problem when I migrated a project form Eclipse to Android Studio.

What you need to do is, remove .9 from the image file name and then, open it in draw9patch tool (located in your /sdk/tools directory) and now, click on Show Bad Patches button on the right upper corner.

You should then see the bad pixels and areas marked in red, you need to do some work there until you have no red bad patches. For me, I just marked one pixel on the middle left edge, and one pixel on middle upper edge, and marked almost all of the right edge and bottom edge. click button again and see that you have no bad patches.

Save the file as .9.png and put it back in the res folder of your project. Android Studio should then build your project with no problem for 9 patch iamges.

Formalize answered 21/11, 2014 at 15:42 Comment(2)
Are you sure it'd work ? Because my app was working fine earlier.Unglue
Probably, Android Studio is less forgiving than Eclipse when it comes to 9 patch. If you had errors with 9 patch when you worked on Eclipse, probably Eclipse will take them with no issue but when you move them to Android Studio it will force you to fix them.Formalize
P
5

In my case I mistakenly extended the black border into the top left corner. Make sure the corners are transparent.

Plumbaginaceous answered 18/11, 2013 at 5:2 Comment(1)
In my case I mistakenly removed the black bottom and right borders... I added borders and it resolved. So I recommend you to check your nine patch image.Edgaredgard
E
4

Now there are two PNG crunchers in the Android build tool, AAPT and a Java cruncher. Both checks for malformed 9 patch images.

I've looked into the source code of the build tool. There is no option to disable compression on 9 patch image or ignore malformed ones. At least not options available to users.

So here is a simple script to replace AAPT:

https://gist.github.com/ksc91u/37513796b2cec37bb3c5

When called to compress a 9 patch image, it will simply copy the file, otherwise, it will launch the real AAPT with @ARGV.

Eyler answered 17/9, 2014 at 9:41 Comment(0)
C
2

If you are making the image using an online generator tool, then you must remove any special characters from the name for it to work.

Colson answered 30/12, 2015 at 13:51 Comment(0)
K
1

Just for the record: For me it turned out that this error came from a black border line on the right hand site and bottom side. So I had a black border on every side (top, right, bottom and left). Removing the redundant right and bottom line worked like a charm.

Keithakeithley answered 15/11, 2013 at 12:1 Comment(0)
Y
1

Your 9-patch can be wrong.

Maybe you strip 9-patches one-pixel borders away and information gained from them is not available. Try to add a bottom line and a right line for text too. I think it will help to solve your problem.

Yokel answered 14/10, 2014 at 13:22 Comment(0)
C
1

For me the problem seems to be that I created the 9 patch myself. I thought it was enough to have a 1 px border on the left and top edges, but you need to make sure that the image has a 1 px border on each edge.

Cheke answered 20/12, 2014 at 11:20 Comment(0)
C
0

When the error comes up it opens the image in android studio as well. All I did to fix this was extend any of the patch borders by a single drawn pixel. I re-ran and it worked. Not sure why, but I repeat tested this with several images.

Chumash answered 4/9, 2014 at 21:0 Comment(0)
S
0

When we nine patch any image it creates a black line to its border. That border should be transparent or completely solid. If it is not, this error will come.

So the solution is to increase the width and height by two pixels. (In Photoshop increase the size of the canvas, not the image.)

Senhorita answered 5/10, 2015 at 10:2 Comment(0)
K
0

I have resolved it by changing the stretch scope in Android Studio.

Kaila answered 12/10, 2016 at 1:57 Comment(0)
K
0

I found the issue is about *.9.png files. Open and check all your 9-Patch files, make sure that all files have black 1 pixel wide border, if don't have, just click the white place and add it, then save it.

It's the only solution to my problem.

Khadijahkhai answered 8/6, 2017 at 6:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.