Drawable,Bitmap - mayurparmar2/AlarmDemo GitHub Wiki
Basic.java
public static Drawable getDrawablefromAssetByName(Context context, String drawableName) {
AssetManager assetManager = context.getAssets();
String imagePath = "images/"+drawableName+".png"; // Replace with your actual image path
try {
InputStream inputStream = assetManager.open(imagePath);
Drawable drawable = Drawable.createFromStream(inputStream, null);
return drawable;
// Now, inputStream contains the image data
} catch (IOException e) {
Log.e("MTAG", "getDrawableResourceIdByName" +e);
try {
String imagePath2 = "images/"+drawableName+".jpg";
InputStream inputStream = assetManager.open(imagePath2);
Drawable drawable = Drawable.createFromStream(inputStream, null);
return drawable;
// Now, inputStream contains the image data
} catch (IOException e2) {
Log.e("MTAG", "getDrawableResourceIdByName jpg: " +e2);
e2.printStackTrace();
}
e.printStackTrace();
return null;
// Handle the exception, e.g., file not found
}
}
Basic.java
/*set Drawable Tint Color */
Drawable drawable= DrawableCompat.wrap(getResources().getDrawable(R.drawable.resourcesid));
DrawableCompat.setTint(drawable, ContextCompat.getColor(context, R.color.white));
## set Bitmap .Bitmap .java
/*set Drawable Tint Color */
Bitmap myLogo = BitmapFactory.decodeResource(context.getResources(), R.drawable.my_drawable);
## set Resize Bitmap .Basic.java
/* Resize */
private Bitmap getResizeBitmap(Bitmap bitmap, int imageHeight, int imageWidth) {
int height = bitmap.getHeight();
int width = bitmap.getWidth();
Matrix matrix = new Matrix();
matrix.postScale(((float)imageWidth) / width, ((float)imageHeight) / height);
return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, false);
}
## save Bitmap .saveBitmap.java
public static boolean saveBitmap(String savePath, Bitmap mBitmap) {
try {
File filePic = new File(savePath);
if (!filePic.exists()) {
filePic.getParentFile().mkdirs();
filePic.createNewFile();
}
FileOutputStream fos = new FileOutputStream(filePic);
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}