Код: Выделить всё
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;
}
Подробнее здесь: https://stackoverflow.com/questions/785 ... al-storage