Learn To Save and Load External Images in Google Android

韩宜春
2023-12-01

http://www.brighthub.com/mobile/google-android/articles/64048.aspx

 

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: http://www.brighthub.com/mobile/google-android/articles/64048.aspx#ixzz1A3FdTxfW

Introduction

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

<LinearLayout>

<ImageView id=”imageToShow”>

</LinearLayout>

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

if(imageFile.exists()){

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(R.id.imageToShow);

And set the bitmap to this ImageView :

myImage.setImageBitmap(myBitmap);

It is that easy!

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

setContentView(R.layout.my_layout);

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);

bos.flush();

bos.close();

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 ,
imageData . length , options );


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


}


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

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

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

MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}

BufferedOutputStream bos = new BufferedOutputStream (
fileOutputStream );

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

bos . flush ();
bos . close ();

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

return true ;
}

 类似资料:

相关阅读

相关文章

相关问答