Last active
November 21, 2021 17:06
-
-
Save pfieffer/06b03bd5f67b095743719669b5f5652a to your computer and use it in GitHub Desktop.
A Bitmap utility class to resample the image, create temporary file image file, delete the image file, add picture to the gallery, save image to storage and share the image.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BitmapUtils { | |
private static final String FILE_PROVIDER_AUTHORITY = "np.com.ravigarbuja.emojifyme.fileprovider"; | |
/** | |
* Resamples the captured photo to fit the screen for better memory usage. | |
* | |
* @param context The application context. | |
* @param imagePath The path of the photo to be resampled. | |
* @return The resampled bitmap | |
*/ | |
static Bitmap resamplePic(Context context, String imagePath) { | |
// Get device screen size information | |
DisplayMetrics metrics = new DisplayMetrics(); | |
WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); | |
manager.getDefaultDisplay().getMetrics(metrics); | |
int targetH = metrics.heightPixels; | |
int targetW = metrics.widthPixels; | |
// Get the dimensions of the original bitmap | |
BitmapFactory.Options bmOptions = new BitmapFactory.Options(); | |
bmOptions.inJustDecodeBounds = true; | |
BitmapFactory.decodeFile(imagePath, bmOptions); | |
int photoW = bmOptions.outWidth; | |
int photoH = bmOptions.outHeight; | |
// Determine how much to scale down the image | |
int scaleFactor = Math.min(photoW / targetW, photoH / targetH); | |
// Decode the image file into a Bitmap sized to fill the View | |
bmOptions.inJustDecodeBounds = false; | |
bmOptions.inSampleSize = scaleFactor; | |
return BitmapFactory.decodeFile(imagePath); | |
} | |
/** | |
* Creates the temporary image file in the cache directory. | |
* | |
* @return The temporary image file. | |
* @throws IOException Thrown if there is an error creating the file | |
*/ | |
static File createTempImageFile(Context context) throws IOException { | |
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", | |
Locale.getDefault()).format(new Date()); | |
String imageFileName = "JPEG_" + timeStamp + "_"; | |
File storageDir = context.getExternalCacheDir(); | |
return File.createTempFile( | |
imageFileName, /* prefix */ | |
".jpg", /* suffix */ | |
storageDir /* directory */ | |
); | |
} | |
/** | |
* Deletes image file for a given path. | |
* | |
* @param context The application context. | |
* @param imagePath The path of the photo to be deleted. | |
*/ | |
static boolean deleteImageFile(Context context, String imagePath) { | |
// Get the file | |
File imageFile = new File(imagePath); | |
// Delete the image | |
boolean deleted = imageFile.delete(); | |
// If there is an error deleting the file, show a Toast | |
if (!deleted) { | |
String errorMessage = context.getString(R.string.error); | |
Toast.makeText(context, errorMessage, Toast.LENGTH_SHORT).show(); | |
} | |
return deleted; | |
} | |
/** | |
* Helper method for adding the photo to the system photo gallery so it can be accessed | |
* from other apps. | |
* | |
* @param imagePath The path of the saved image | |
*/ | |
private static void galleryAddPic(Context context, String imagePath) { | |
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); | |
File f = new File(imagePath); | |
Uri contentUri = Uri.fromFile(f); | |
mediaScanIntent.setData(contentUri); | |
context.sendBroadcast(mediaScanIntent); | |
} | |
/** | |
* Helper method for saving the image. | |
* | |
* @param context The application context. | |
* @param image The image to be saved. | |
* @return The path of the saved image. | |
*/ | |
static String saveImage(Context context, Bitmap image) { | |
String savedImagePath = null; | |
// Create the new file in the external storage | |
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", | |
Locale.getDefault()).format(new Date()); | |
String imageFileName = "JPEG_" + timeStamp + ".jpg"; | |
File storageDir = new File( | |
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) | |
+ "/Emojify"); | |
boolean success = true; | |
if (!storageDir.exists()) { | |
success = storageDir.mkdirs(); | |
} | |
// Save the new Bitmap | |
if (success) { | |
File imageFile = new File(storageDir, imageFileName); | |
savedImagePath = imageFile.getAbsolutePath(); | |
try { | |
OutputStream fOut = new FileOutputStream(imageFile); | |
image.compress(Bitmap.CompressFormat.JPEG, 100, fOut); | |
fOut.close(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
// Add the image to the system gallery | |
galleryAddPic(context, savedImagePath); | |
// Show a Toast with the save location | |
String savedMessage = context.getString(R.string.saved_message, savedImagePath); | |
Toast.makeText(context, savedMessage, Toast.LENGTH_SHORT).show(); | |
} | |
return savedImagePath; | |
} | |
/** | |
* Helper method for sharing an image. | |
* | |
* @param context The image context. | |
* @param imagePath The path of the image to be shared. | |
*/ | |
static void shareImage(Context context, String imagePath) { | |
// Create the share intent and start the share activity | |
File imageFile = new File(imagePath); | |
Intent shareIntent = new Intent(Intent.ACTION_SEND); | |
shareIntent.setType("image/*"); | |
Uri photoURI = FileProvider.getUriForFile(context, FILE_PROVIDER_AUTHORITY, imageFile); | |
shareIntent.putExtra(Intent.EXTRA_STREAM, photoURI); | |
context.startActivity(shareIntent); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ContentProvider
needs to be separately created.