Save Image Into SD Card In Android

In this tutorial, you will learn how to save an image into your internal device SD Card. By default, images are usually stored to an internal device SD Card. We will create an actionbar menu and on menu item click will create a folder and save an image into the internal device SD Card. So lets begin…

Create a new project in Eclipse File > New > Android Application Project. Fill in the details and name your project SaveImageTutorial.

Application Name : SaveImageTutorial

Project Name : SaveImageTutorial

Package Name : com.androidbegin.saveimagetutorial

Open your MainActivity.java and paste the following code.

MainActivity.java

package com.androidbegin.saveimagetutorial;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import android.os.Bundle;
import android.os.Environment;
import android.app.ActionBar;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Get the view from activity_main.xml
		setContentView(R.layout.activity_main);

		// Create an actionbar
		ActionBar actionBar = getActionBar();
		actionBar.show();

		// Locate ImageView in activity_main.xml
		ImageView myimage = (ImageView) findViewById(R.id.image);

		// Attach image into ImageView
		myimage.setImageResource(R.drawable.wallpaper);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Create an actionbar menu
		menu.add("Save Image")
				// Add a new Menu Button
				.setOnMenuItemClickListener(this.SaveImageClickListener)
				.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

		return super.onCreateOptionsMenu(menu);
	}

	// Capture actionbar menu item click
	OnMenuItemClickListener SaveImageClickListener = new OnMenuItemClickListener() {

		public boolean onMenuItemClick(MenuItem item) {

			// TODO Auto-generated method stub

			Bitmap bitmap;
			OutputStream output;

			// Retrieve the image from the res folder
			bitmap = BitmapFactory.decodeResource(getResources(),
					R.drawable.wallpaper);

			// Find the SD Card path
			File filepath = Environment.getExternalStorageDirectory();

			// Create a new folder in SD Card
			File dir = new File(filepath.getAbsolutePath()
					+ "/Save Image Tutorial/");
			dir.mkdirs();

			// Create a name for the saved image
			File file = new File(dir, "myimage.png");

			// Show a toast message on successful save
			Toast.makeText(MainActivity.this, "Image Saved to SD Card",
					Toast.LENGTH_SHORT).show();
			try {

				output = new FileOutputStream(file);

				// Compress into png format image from 0% - 100%
				bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
				output.flush();
				output.close();
			}

			catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return false;
		}
	};
}

We have created an ImageView to show the attached image on the graphical layout and an actionbar menu. On actionbar menu item click will save the specified image into the internal SD Card.

We have prepared a sample image for this tutorial. Insert your downloaded sample image into your res > drawable-hdpi.

Sample Image

[wpfilebase tag=file id=78 tpl=download-button /]

Next, create an XML file for your MainActivity Graphical Layout. Go to res > layout > Right Click on layout > New >Android XML File

Name your new XML file activity_main.xml and paste the following code.

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

Next, change the application name. Open your strings.xml in your res > values folder and paste the following code.

strings.xml

<resources>

    <string name="app_name">Save Image Tutorial</string>
    <string name="menu_settings">Settings</string>

</resources>

In your AndroidManifest.xml, we need to declare a permission to allow the application to write an external storage. Open your AndroidManifest.xml and paste the following code.

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.androidbegin.saveimagetutorial"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="15" />

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:uiOptions="splitActionBarWhenNarrow" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Output:

Save Image Tutorial ScreenShots

Source Code 

[purchase_link id=”7829″ text=”Purchase to Download Source Code” style=”button” color=”green”]

Latest comments

How do we get it from the url

OldMonker

Save Image Into SD Card In Android

add this; MediaScannerConnection.scanFile(this, new String[] { sdImageMainDirectory.getAbsolutePath() }, null, null);

Amar Skenderovic

Save Image Into SD Card In Android

the image will not be shown in the gallery, how to make it available in the gallery?

Mayank Langalia

Save Image Into SD Card In Android

hi, thanks for the tutorial really helpful, but the image will not be shown in the gallery, how to make it available in the gallery?

Reham Arnaout

Save Image Into SD Card In Android