9-patch image error in Android

AndroidNine PatchAapt

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

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.

Solution 3 - Android

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.

Solution 4 - Android

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.

Solution 5 - Android

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

Solution 6 - Android

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.

Solution 7 - Android

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.

Solution 8 - Android

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.

Solution 9 - Android

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.

Solution 10 - Android

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.

Solution 11 - Android

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.

Solution 12 - Android

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.)

Solution 13 - Android

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

Solution 14 - Android

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHeberView Question on Stackoverflow
Solution 1 - AndroidJiawei DaiView Answer on Stackoverflow
Solution 2 - Androidchange_is_necessityView Answer on Stackoverflow
Solution 3 - AndroidReuben ScrattonView Answer on Stackoverflow
Solution 4 - AndroidMoti BartovView Answer on Stackoverflow
Solution 5 - Androidbradley4View Answer on Stackoverflow
Solution 6 - Androidksc91uView Answer on Stackoverflow
Solution 7 - AndroidmbpatelView Answer on Stackoverflow
Solution 8 - AndroidLangusten GustelView Answer on Stackoverflow
Solution 9 - AndroidQAreaView Answer on Stackoverflow
Solution 10 - AndroidGuido KerstenView Answer on Stackoverflow
Solution 11 - AndroiddbconfessionView Answer on Stackoverflow
Solution 12 - AndroidDhanraj GullapalliView Answer on Stackoverflow
Solution 13 - AndroidchefishView Answer on Stackoverflow
Solution 14 - AndroidAkash BisariyaView Answer on Stackoverflow