How to Save a Bitmap on Internal Storage

How to save a bitmap on internal storage

To Save your bitmap in sdcard use the following code

Store Image

private void storeImage(Bitmap image) {
File pictureFile = getOutputMediaFile();
if (pictureFile == null) {
Log.d(TAG,
"Error creating media file, check storage permissions: ");// e.getMessage());
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
image.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.close();
} catch (FileNotFoundException e) {
Log.d(TAG, "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d(TAG, "Error accessing file: " + e.getMessage());
}
}

To Get the Path for Image Storage

/** Create a File for saving an image or video */
private File getOutputMediaFile(){
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir = new File(Environment.getExternalStorageDirectory()
+ "/Android/data/"
+ getApplicationContext().getPackageName()
+ "/Files");

// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.

// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmm").format(new Date());
File mediaFile;
String mImageName="MI_"+ timeStamp +".jpg";
mediaFile = new File(mediaStorageDir.getPath() + File.separator + mImageName);
return mediaFile;
}

EDIT
From Your comments i have edited the onclick view in this the button1 and button2 functions will be executed separately.

public onClick(View v){

switch(v.getId()){
case R.id.button1:
//Your button 1 function
break;
case R.id. button2:
//Your button 2 function
break;
}
}

How i can save bitmap to internal storage?

Do you add <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> in AndroidManifest file ?

UPDATE:
if you want to save to internal storage, you can use below code:

private void storeImage(Bitmap image) {
try {
String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmm").format(new Date());
String mImageName="MI_"+ timeStamp +".jpg";

FileOutputStream fos = openFileOutput(mImageName, MODE_PRIVATE);
image.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

it will save to data/data/your_packagename/files

Saving and Reading Bitmaps/Images from Internal memory in Android

Use the below code to save the image to internal directory.

private String saveToInternalStorage(Bitmap bitmapImage){
ContextWrapper cw = new ContextWrapper(getApplicationContext());
// path to /data/data/yourapp/app_data/imageDir
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);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return directory.getAbsolutePath();
}

Explanation :

1.The Directory will be created with the given name. Javadocs is for to tell where exactly it will create the directory.

2.You will have to give the image name by which you want to save it.

To Read the file from internal memory. Use below code

private void loadImageFromStorage(String path)
{

try {
File f=new File(path, "profile.jpg");
Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
ImageView img=(ImageView)findViewById(R.id.imgPicker);
img.setImageBitmap(b);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}

}

How to save images from bitmap into Storage in android

Amirouche,Here is the sample code for saving bitmap to file :

public static File savebitmap(Bitmap bmp) throws IOException {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 60, bytes);
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "testimage.jpg");
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();
return f;
}

Now call this function to save the bitmap to internal memory.

File newfile = savebitmap(bitmap);

I hope it will help you.
Happy codeing life.

How to save bitmap on internal storage download from internet

You can save and load the image on the internal storage like this:
Saving:

public static void saveFile(Context context, Bitmap b, String picName){ 
FileOutputStream fos;
try {
fos = context.openFileOutput(picName, Context.MODE_PRIVATE);
b.compress(Bitmap.CompressFormat.PNG, 100, fos);
}
catch (FileNotFoundException e) {
Log.d(TAG, "file not found");
e.printStackTrace();
}
catch (IOException e) {
Log.d(TAG, "io exception");
e.printStackTrace();
} finally {
fos.close();
}
}

Loading:

public static Bitmap loadBitmap(Context context, String picName){ 
Bitmap b = null;
FileInputStream fis;
try {
fis = context.openFileInput(picName);
b = BitmapFactory.decodeStream(fis);
}
catch (FileNotFoundException e) {
Log.d(TAG, "file not found");
e.printStackTrace();
}
catch (IOException e) {
Log.d(TAG, "io exception");
e.printStackTrace();
} finally {
fis.close();
}
return b;
}

But you will need to save the imageName somehow if you want to find it again if the app is closed. I would recomend an SQLLite database that map imageNames to entries in the database.

(Android Studio) How to save bitmap to Internal Storage?

Try doing this and then use the data to save it in your local device.

saveImageBitmap(Bitmap bitmap){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 20, baos);
byte[] data = baos.toByteArray();

//do whatever you want to save this bitmap file
}

Hope that'll help you out! This will transform the bitmap into .jpg format and then save it in your file.



Related Topics



Leave a reply



Submit