When my app first starts, it lets the user select a profile picture. This can be done taking a photo at the moment, or selecting it from the gallery.
After the user
Try this ...
You should use the Bitmap.compress() method to save a Bitmap as a file. It will compress (if the format used allows it) your picture and push it into an OutputStream.
Here is an example of a Bitmap instance obtained through getImageBitmap(myurl) that can be compressed as a JPEG with a compression rate of 85% :
String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOut = null;
File file = new File(path, "FitnessGirl"+Contador+".jpg"); // the File to save to
fOut = new FileOutputStream(file);
Bitmap pictureBitmap = getImageBitmap(myurl); // obtaining the Bitmap
pictureBitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut); // saving the Bitmap to a file compressed as a JPEG with 85% compression rate
fOut.flush();
fOut.close(); // do not forget to close the stream
MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
You can use this imagesaver class for save bitmap image to your app folder The image saver class code is given below
public class ImageSaver {
private String directoryName = "images";
private String fileName = "image.png";
private Context context;
private File dir;
private boolean external=false;
public ImageSaver(Context context) {
this.context = context;
}
public ImageSaver setFileName(String fileName) {
this.fileName = fileName;
return this;
}
public ImageSaver setExternal(boolean external) {
this.external = external;
return this;
}
public ImageSaver setDirectory(String directoryName) {
this.directoryName = directoryName;
return this;
}
public void save(Bitmap bitmapImage) {
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(createFile());
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fileOutputStream != null) {
fileOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
@NonNull
private File createFile() {
File directory;
if (external) {
directory = getAlbumStorageDir(directoryName);
if (!directory.exists()){
directory.mkdir();
}
} else {
directory = new File(context.getFilesDir()+"/"+directoryName);
if (!directory.exists()){
directory.mkdir();
}
}
return new File(directory, fileName);
}
private File getAlbumStorageDir(String albumName) {
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), albumName);
if (!file.mkdirs()) {
Log.e("ImageSaver", "Directory not created");
}
return file;
}
public static boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
return Environment.MEDIA_MOUNTED.equals(state);
}
public static boolean isExternalStorageReadable() {
String state = Environment.getExternalStorageState();
return Environment.MEDIA_MOUNTED.equals(state) ||
Environment.MEDIA_MOUNTED_READ_ONLY.equals(state);
}
public Bitmap load() {
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(createFile());
return BitmapFactory.decodeStream(inputStream);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public boolean deleteFile() {
File file = createFile();
return file.delete();
}
}
then in your activity where you getting the bitmap from server(by using Glide or Picasso you can use any method)
you should set setExternal
before you call .setDirectory
Bitmap bitmap=........//bitmap from code
new ImageSaver(this)
.setFileName("filename.jpg")
.setExternal(false)//image save in external directory or app folder default value is false
.setDirectory("dir_name")
.save(bitmap); //Bitmap from your code
Well, you need to create the folder if it doesn't exist.
try running this code and see if it fixes your issue:
File parentDestination = saveFile.getParentFile();
if (!parentDestination.exists()) {
parentDestination.mkdirs(); //make all the directory structures needed
}
understand your requirement. Below pasting some piece of code which i tested and working.Basically get the image from camera and save it in app storage.please go through it. Hope this helps.Cheers..
//For saving image...
private String saveToInternalSorage(Bitmap bitmapImage) {
ContextWrapper cw = new ContextWrapper(getApplicationContext());
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
// Create imageDir
File mypath = new File(directory, "profile.jpg");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
// Use the compress method on the BitMap object to write image to
// the OutputStream
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
Editor editor = sharedpreferences.edit();
editor.putString("saved", "na");
editor.commit();
} catch (Exception e) {
e.printStackTrace();
}
return directory.getAbsolutePath();
}
//..To load image from storage
private void loadImageFromStorage(String path) {
try {
ContextWrapper cw = new ContextWrapper(getApplicationContext());
File path1 = cw.getDir("imageDir", Context.MODE_PRIVATE);
File f = new File(path1, "profile.jpg");
Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
ImageView img = (ImageView) findViewById(R.id.viewImage);
img.setImageBitmap(b);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
After trying several things, this is what finally has worked for me:
ContextWrapper cw = new ContextWrapper(getApplicationContext());
File directory = cw.getDir("profile", Context.MODE_PRIVATE);
if (!directory.exists()) {
directory.mkdir();
}
File mypath = new File(directory, "thumbnail.png");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
resizedbitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (Exception e) {
Log.e("SAVE_IMAGE", e.getMessage(), e);
}
Basically the thing is to check if the directory (not the file) exists, and if not, create it with mkdir().