Skip to content

Instantly share code, notes, and snippets.

@JonathandelaSen
Created August 18, 2017 07:06
Show Gist options
  • Save JonathandelaSen/7de287d8619f098033defd19e68c617f to your computer and use it in GitHub Desktop.
Save JonathandelaSen/7de287d8619f098033defd19e68c617f to your computer and use it in GitHub Desktop.
Utility Android Class for saving files
import android.app.Activity;
import android.graphics.Bitmap;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class UtilsSaveFile {
public static File createFileInAlbum(String albumName, String fileName) {
Log.d(UtilsSaveFile.class.getName(), "createFileInAlbum");
// Get the directory for the user's public pictures directory.
File file = new File(EXTERNAL_DIRECTORY, albumName);
if (!file.mkdirs()) {
Log.d(UtilsSaveFile.class.getName(), "Directory not created");
}
file = new File(file.getAbsolutePath(), fileName);
try {
file.createNewFile();
} catch (IOException e) {
Log.d(UtilsSaveFile.class.getName(), "Error creating new file");
e.printStackTrace();
}
return file;
}
/* Checks if external storage is available for read and write */
public boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}
/* Checks if external storage is available to at least read */
public boolean isExternalStorageReadable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state) ||
Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return true;
}
return false;
}
public static void addImageToGallery(final String filePath, Activity activity) {
MediaScannerConnection.scanFile(
activity, new String[] { filePath }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.d(UtilsSaveFile.class.toString(), "FilePath " + filePath);
Log.d(UtilsSaveFile.class.toString(), "Finished scanning " + path + " New row: " + uri);
}
} );
}
public static File getFileFromGallery(String fileName) {
return new File(ALBUM_PATH + "/" + fileName);
}
/**
* @param fileName The file name.
* @param bm The Bitmap you want to save.
* @return true if the Bitmap was saved successfully, false otherwise.
*/
public static boolean saveBitmapToFile(String fileName, Bitmap bm) {
File imageFile = new File(ALBUM_PATH, fileName);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(imageFile);
bm.compress(Bitmap.CompressFormat.JPEG, QUALITY_BITMAP_FROM_FILE, fos);
fos.close();
return true;
}
catch (IOException e) {
Log.e(UtilsSaveFile.class.toString(), e.getMessage());
if (fos != null) {
try {
fos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment