In this article, we will see how to load a Image from an SD card to our Android project and show how to save it from our project to the SD card.

Read more:


This article is a complement to the How to Program the Android Camera to Take Pictures . Now let's have a look at two basic functions we can use when we are working with media files: Loading images from the SD card, and Saving images to the SD card.

The scenarios are the following:

We have an image in our SD card, placed in our application folder, and we want to use it in our Android project as a background, for example. In the “Load Images” paragraph we will cover this case.

In the other case we have taken a picture (as I explained in the How to Program the Android Camera to Take Pictures tutorial) and we want to save it to the SD card. The “Saving images” paragraph will cover this.

Loading Images

First of all, let's “set the environment”:

We have our image in the path /sdcard/myImages/myImage.jpg and we want to put it as a background in a layout. The layout structure is not important, just keep in mind that it is a


<ImageView id=”imageToShow”>


Let's begin!

To check if the file exists, let's do first a little confirmation: We need to create a File object with our image. We can do this using the following piece of code:

File imageFile = new File(“/sdcard/myImages/myImage.jpg”);

Tip! In my point of view, it's important to check everything we do. In this case, after we start working with the imageFile object we should assure ourselves that it exists. Something like


go on....


Now, let's create a Bitmap object from our image path.

Bitmap myBitmap = BitmapFactory.decodeFile(“/sdcard/myImages/myImage.jpg”);

Here, we have the image stored in a bitmap object in our android code. Let's place this image as background in your imageview in the layout. To do this, we need to create a ImageView android object linked to the XML.

ImageView myImage = (ImageView) findViewById(;

And set the bitmap to this ImageView :


It is that easy!

Hint! This code has to be placed in an activity, and this activity must have a


Otherwise, it won't work.

Saving Images

Now we'll look at the case where we take a picture with the camera using the 'How to Program the Android Camera to Take Pictures' tutorial. When we take a picture, we receive a byte array... what do we do with this? How can we convert this to a image in our SD card? Let's do it.

We create a File object with the place where to store the images.

File sdImageMainDirectory = "/sdcard/myImages";

We initialize some variables.

FileOutputStream fileOutputStream = null;

The image file name

String nameFile = “myImage”

Now, the quality of the image. This is a value between 0 and 100. 0 meaning compress for small size, 100 meaning compress for max quality. Some formats, like PNG which is lossless, will ignore the quality setting (via Google Android Reference )

int quality = 50;

We create the options we are going to use in our compression (adding the sample size)

BitmapFactory.Options options=new BitmapFactory.Options();

options.inSampleSize = 5;

We create the Bitmap from the imageData (byte array) and we throw it to the FileOutputStream with the name and the compression given (In this case JPEG)

Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0,imageData.length,options);

fileOutputStream = new FileOutputStream(sdImageMainDirectory.toString() +"/" + nameFile + ".jpg");

BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);

myImage.compress(CompressFormat.JPEG, quality, bos);



At this point, we will have the image stored in our SD card. Here we can play with the quality and sample values. How will the image look like with different values? You can try...

sample code:
public static boolean StoreByteImage(Context mContext, byte[] imageData,
int quality, String expName) {

File sdImageMainDirectory = new File("/sdcard/myImages");
FileOutputStream fileOutputStream = null;
String nameFile;
try {

BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 5;

Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0,

fileOutputStream = new FileOutputStream(
sdImageMainDirectory.toString() +"/" + nameFile + ".jpg");


private void saveToFile(Bitmap bmp){
String fileName = "tmp.png";
FileOutputStream out = new FileOutputStream(fileName);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
}catch(Exception e){

private void saveToSDCard(Bitmap bmp){
String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOut = null;
File file = new File(path, "tmp" + ".png");
fOut = new FileOutputStream(file);

bmp.compress(Bitmap.CompressFormat.JPEG, 85, fOut);

}catch(FileNotFoundException e){
}catch(IOException e){

BufferedOutputStream bos = new BufferedOutputStream(

myImage.compress(CompressFormat.JPEG, quality, bos);


} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block

return true;


  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer


  1. Android 桌面组件widget
  2. Android——LinearLayout按下(state_pres
  3. Android推送服务(GCM)----GCM Architectu
  4. Android studio3修改打包出來的名字
  5. 谷歌开发工具Android Studio安装使用图文
  6. 【Android】 使用ADT16出现ImageView War
  7. (Android jni) error: base operand of '->
  8. Android事件分发-总结
  9. Android最佳实践之Notification、下拉刷
  10. 关于 android 平台上的 usb 投屏