Bitmap - mayurparmar2/AlarmDemo GitHub Wiki
test.java
public static String overwriteImageWithBitmap(Context context, Bitmap bitmap, String filePath) {
try {
File file = new File(filePath);
// Ensure the file and its parent directory exist
file.getParentFile().mkdirs();
// Convert the Bitmap to a FileOutputStream
FileOutputStream outputStream = new FileOutputStream(file);
// Compress and save the Bitmap to the file
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
// Close the output stream
outputStream.close();
// Notify the MediaScanner to update the file in the gallery (optional)
// This step is optional and useful if you want the new image to appear in the gallery immediately.
// It requires the WRITE_EXTERNAL_STORAGE permission.
android.media.MediaScannerConnection.scanFile(
context,
new String[]{file.getAbsolutePath()},
null,
null
);
Log.e("MTAG", "overwriteImageWithBitmap sucesss ");
// You can also update the image in an ImageView or perform other actions as needed.
} catch (IOException e) {
e.printStackTrace();
Log.e("MTAG", "overwriteImageWithBitmap IOException " + e);
}
return filePath;
}
public static void main(String[] args) {
// Example usage
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.my_bitmap);
String fileName = "my_image.png";
overwriteImageWithBitmap(getApplicationContext(), bitmap, fileName);
}
}