Monday, 24 December 2012

Save Bitmap in Internal and External Memory in Android


/**
* Method to save bitmap into internal memory
* @param image and context
*/
public boolean saveImageToInternalStorage(Bitmap image,Context context)
{
try {
FileOutputStream fos = context.openFileOutput("photo.jpg", Context.MODE_WORLD_READABLE);
image.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                // 100 means no compression, the lower you go, the stronger the compression
fos.close();
return true;
}
catch (Exception e) {
Log.e("saveToInternalStorage()", e.getMessage());
}
return false;
}

/**
 * Method to save bitmap into external storage
* @param image
* @return true if save success otherwise false
*/
public void saveImageToExternalStorage(Bitmap image) {
//image=scaleCenterCrop(image,200,200);
String fullPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/";
try
{
File dir = new File(fullPath);
if (!dir.exists()) {
dir.mkdirs();
}
OutputStream fOut = null;
File file = new File(fullPath, "photo1.png");

if(file.exists())
file.delete();

file.createNewFile();
fOut = new FileOutputStream(file);
// 100 means no compression, the lower you go, the stronger the compression
image.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
}
catch (Exception e)
{
Log.e("saveToExternalStorage()", e.getMessage());
}
}

No comments:

Post a Comment