Hello Guys, Here I am going to show the sample code of how to Save Image into SD Card.
Its very easy to save any image into your SD Card.
Very first you must give a permission into Menifest file for saving image.
Permission :
<uses-permission android:name= "android.permission.WRITE_EXTERNAL_STORAGE"/>
Sample Code:
File: ImageDownloadingActivity.java
Bitmap bitmap = null;
OutputStream output;
bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.android_icon);
// Find the SD Card path
File filepath = Environment.getExternalStorageDirectory();
// Create a new folder in SD Card
File dir = new File(filepath.getAbsolutePath() + "/Save Image/");
dir.mkdirs();
File file = new File(dir, "Img_icon.png");
// Show a toast message on successful save
Toast.makeText(ImageDownloadingActivity.this, "Image Saved to SD Card", Toast.LENGTH_SHORT).show();
try
{
output = new FileOutputStream(file);
//choose another format if PNG doesn't suit you
bitmap.compress(CompressFormat.PNG, 100, output);
output.flush();
output.close();
}
catch (FileNotFoundException e)
{
Log.w("TAG", "Error saving image file: " + e.getMessage());
}
catch (IOException e)
{
Log.w("TAG", "Error saving image file: " + e.getMessage());
}
Output:
If you run this app into your device then you must have to find the folder name "Save Image" into your device. In that folder you get the downloaded image from your app.
If you run app into emulator then you can see the downloaded image into File Explorer. Just see the following image.
Download Source Code: ImageDownloding
Its very easy to save any image into your SD Card.
Very first you must give a permission into Menifest file for saving image.
Permission :
<uses-permission android:name= "android.permission.WRITE_EXTERNAL_STORAGE"/>
Sample Code:
File: ImageDownloadingActivity.java
Bitmap bitmap = null;
OutputStream output;
bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.android_icon);
// Find the SD Card path
File filepath = Environment.getExternalStorageDirectory();
// Create a new folder in SD Card
File dir = new File(filepath.getAbsolutePath() + "/Save Image/");
dir.mkdirs();
File file = new File(dir, "Img_icon.png");
// Show a toast message on successful save
Toast.makeText(ImageDownloadingActivity.this, "Image Saved to SD Card", Toast.LENGTH_SHORT).show();
try
{
output = new FileOutputStream(file);
//choose another format if PNG doesn't suit you
bitmap.compress(CompressFormat.PNG, 100, output);
output.flush();
output.close();
}
catch (FileNotFoundException e)
{
Log.w("TAG", "Error saving image file: " + e.getMessage());
}
catch (IOException e)
{
Log.w("TAG", "Error saving image file: " + e.getMessage());
}
Output:
If you run this app into your device then you must have to find the folder name "Save Image" into your device. In that folder you get the downloaded image from your app.
If you run app into emulator then you can see the downloaded image into File Explorer. Just see the following image.
Download Source Code: ImageDownloding
Nice Example...
ReplyDelete