For Beginner, today we learn how to save image on sdcard,
n 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
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.
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
1
2
3
4
5
6
7
8
9
10
11
|
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
1
2
3
4
5
6
|
<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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
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>
|