I have used same 9 patch image in drawable folder where it was working fine but when i saved it in internal storage it is treated as regular image with black lines(of 9-patch tool) shown in image.
public static Drawable getImageDrawable(Context context, String themeName, String imageName) {
File themeDir = new File(context.getFilesDir(), THEME_DIR);
File themeSubDir = new File(themeDir, themeName);
if (themeSubDir.exists() && themeSubDir.isDirectory()) {
File imageFile = new File(themeSubDir, imageName);
if (imageFile.exists() && imageFile.isFile()) {
if (imageName.endsWith(".9.png")) {
return getNinePatchDrawable(context, imageFile);
} else {
return Drawable.createFromPath(imageFile.getAbsolutePath());
}
}
}
return null; // Return null if the file does not exist
}
private static Drawable getNinePatchDrawable(Context context, File imageFile) {
try {
FileInputStream fis = new FileInputStream(imageFile);
Bitmap bitmap = BitmapFactory.decodeStream(fis);
if (bitmap != null) {
byte[] chunk = bitmap.getNinePatchChunk();
if (chunk != null) {
String chunkData = new String(chunk, StandardCharsets.UTF_8);
Log.d("TAG", "Chunk data: " + chunkData);
}
if (NinePatch.isNinePatchChunk(chunk)) {
Rect padding = new Rect();
NinePatchDrawable ninePatchDrawable = new NinePatchDrawable(context.getResources(), bitmap, chunk, padding, null);
return ninePatchDrawable;
} else {
Log.e("CheckPoint1", "The chunk is not a valid nine-patch chunk: " + imageFile.getAbsolutePath());
}
} else {
Log.e("CheckPoint2", "Bitmap could not be decoded: " + imageFile.getAbsolutePath());
}
} catch (Exception e) {
Log.e("CheckPoint3", "Error reading image file: " + imageFile.getAbsolutePath(), e);
}
return null;
}
in case of 9-patch images it always ends at checkPoint1, i have tried many images. when i use drawable class it shows image but content and stretching area is works as regular image but when i saved these images in drawable folder to check whether image is faulty, i got all the images fine.
Syed Mubashshir Ahmad is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.