Sometime we need to save images on SDcard that we click or get from server in many applications, and you can do it very easily by using the below code.
To save Images on SDcard on Android Device, write the following code:
ByteArrayOutputStream bos = new ByteArrayOutputStream();
// Here imageBitmap is the bitmap of image that we want to save on SDcard
imageBitmap.compress(CompressFormat.JPEG, 75, bos);
// Here you will provide the folder name
File photoDirectory= new File(android.os.Environment.getExternalStorageDirectory()+"/Varta/images");
if(!photoDirectory.exists())
{
photoDirectory.mkdirs();
}
File photo= new File(android.os.Environment.getExternalStorageDirectory()+"/Varta/images", new Date().getTime()+".png");
String fileName=photo.getPath();
FileOutputStream fos;
try
{
fos = new FileOutputStream(fileName);
bos.writeTo(fos);
bos.flush();
fos.flush();
bos.close();
fos.close();
return fileName;
} catch (FileNotFoundException e)
{
e.printStackTrace();
return null;
} catch (IOException e)
{
e.printStackTrace();
return null;
}
Hope this will help you :)
0 Comment(s)