How to save Jpeg files in the Android emulator

I am learning the basics of graphics in Android. I started with the Fingerpaint activity in Api Demos. What I wanted to learn:

  • how to turn a view into a jpeg (jpg) file
  • how to save that on an Android device
  • how to set the background of a view from an image

So far I have something working on the emulator. I have not tried it yet on a real device.

1. Create an Android Virtual Device (avd) and assign a value for SD card memory. The figure below  shows an assignment of 128MB.

AVD details with SD card memory

2. Give permission to your application so it is allowed to write to external storage. Edit the manifest file AndroidManifest.xml. Add uses-permission as the first item in the manifest element.

<uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE”>

3. In the activity and view code, create files and folders. (See below for attached source code.)

File myDir=new File("/sdcard/saved_images");
 String fname = "image-" + mImageCount + ".jpg";
 File file = new File (myDir, fname);
 if (file.exists ()) file.delete ();
 mView.saveAsJpg (file);

public void saveAsJpg (File f) {
String fname = f.getAbsolutePath ();
FileOutputStream fos = null;
try {
fos = new FileOutputStream (f);
// Take the bitmap of the view and write it out as a jpeg.
mBitmap.compress (CompressFormat.JPEG, 95, fos);
} catch (Throwable ex) {


} // end saveAsJpg

4. Run your application and draw something on the screen. Then use the “save” menu item to save your file.

5. To see the file that you created, you have to open the DDMS window in Eclipse while the emulator is running. If you go the “File Explorer” tab and look in the /mnt/sdcard folder. If you actually want the file, you use the little icon to the right of the tabs. Click the “Pull a file from device” after you highlight the file in the file list.

Where To Find The Code

The Eclipse project for this can be found here: download zip file. If you find that the app does not build in Eclipse, be sure to do a clean build by using the “Clean” item on the Project menu.

When running in the emulator, it should look like this:

About This Work
This work was done on a Macbook with Eclipse Galileo and Android version 2.2. In Eclipse, when you list “installation details” under “About Eclipse” it shows version 0.9.9.v201009221407-60953 of the Android Developer Tools.


About Bill Lahti

Bill Lahti is a software engineer building mobile applications and knowledge management solutions. Two of his interests are writing for this blog and building Android apps, with strategy games being an area of particular interest.
This entry was posted in Android and tagged , , , . Bookmark the permalink.

7 Responses to How to save Jpeg files in the Android emulator

  1. WhoSoft says:

    Ive been trying to figure out how to do that for the past 3 days and its been driving me crazy…..Thanks for the little tutorial.

  2. Dieni says:

    A nice tutorial. Do you know how to insert text to speech to our application on andorid?

  3. Ashwini says:

    in my appliction i want to save map which is displayed in emulator to to save map to you are using myview with using myview is it possible to save map to sdcard.

  4. Katariya says:

    I used this code but not work properly

    • blahti says:

      That is something I worked on when I was first getting started with Android. I will take a look at it in the next week and see why it is used to work on Android 2.1 but no longer. Thanks for calling it to my attention.

      I have learned a lot about about bitmaps and how to work with them. A resource you might find useful is “Loading Large Bitmaps Efficiently“. It is an excellent example; it includes examples of reading bitmaps to and from files.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.