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.
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
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
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(R.id.imageToShow);
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_._
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…
This is the core functionality, but the full source code can be found in my Code Snippets.
Ask, comment, interact!
Ask any question you have about this article, and I will try to answer as fast as I can. Comment my code, my writing; maybe there is something that it is missing or is not complete, just let me know!! In other words, interact!!
If you want to know when new articles are released, subscribe yourself to the Google Android RSS. Otherwise, you can follow my research, articles and work in my professional twitter: jbeerdev
This post is part of the series: More Android Elements!!
More information about Android and its programing environment.