Thursday, February 17, 2011

Display Gallery selected image using BitmapFactory

In last exercise "Select Image using Android build-in Gallery", we ask Android build-in Gallery app help to select image, with uri returned. In this exercise, BitmapFactory is used to load the image.

Display Gallery selected image using BitmapFactory

Modify main.xml to add a ImageView to display the returned image
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 >
<TextView
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="@string/hello"
 />
<Button
 android:id="@+id/loadimage"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="Load Image"
 />
<TextView
 android:id="@+id/targeturi"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 />
<ImageView
 android:id="@+id/targetimage"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 />
</LinearLayout>


Modify the code to load image using BitmapFactory.
package com.exercise.AndroidSelectImage;

import java.io.FileNotFoundException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class AndroidSelectImage extends Activity {

TextView textTargetUri;
ImageView targetImage;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
     Button buttonLoadImage = (Button)findViewById(R.id.loadimage);
     textTargetUri = (TextView)findViewById(R.id.targeturi);
     targetImage = (ImageView)findViewById(R.id.targetimage);
  
     buttonLoadImage.setOnClickListener(new Button.OnClickListener(){

 @Override
 public void onClick(View arg0) {
  // TODO Auto-generated method stub
  Intent intent = new Intent(Intent.ACTION_PICK,
    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  startActivityForResult(intent, 0);
 }});
 }

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);

if (resultCode == RESULT_OK){
 Uri targetUri = data.getData();
 textTargetUri.setText(targetUri.toString());
 Bitmap bitmap;
 try {
  bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
  targetImage.setImageBitmap(bitmap);
 } catch (FileNotFoundException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
}
}
}


Remark@2012-07-14: Recently found that it cannot display large image, such as original photos from camera ~ but I don't know why no error reported!!! Refer to the update post: Scale bitmap Efficiently.

Related:
- Convert URI to real path format
- Read EXIF of JPG file

3 comments:

lse123 said...

what android version run this? I tried 2.3.3 sdk in emultor(with sd card) but not run?

lse123 said...

what android version run this? I tried 2.3.3 sdk in emultor(with sd card) but not run?
emulator do NOT proceed>>>
[2012-07-13 19:58:42 - AndroidSelectImage] ------------------------------
[2012-07-13 19:58:42 - AndroidSelectImage] Android Launch!
[2012-07-13 19:58:42 - AndroidSelectImage] adb is running normally.
[2012-07-13 19:58:42 - AndroidSelectImage] Performing com.deitel.androidselectimage.AndroidSelectImage activity launch
[2012-07-13 19:58:42 - AndroidSelectImage] Automatic Target Mode: Preferred AVD 'Note_GB' is not available. Launching new emulator.
[2012-07-13 19:58:42 - AndroidSelectImage] Launching a new emulator with Virtual Device 'Note_GB'
[2012-07-13 19:58:42 - Emulator] emulator: warning: opening audio input failed
[2012-07-13 19:58:42 - Emulator]
[2012-07-13 19:58:43 - AndroidSelectImage] New emulator found: emulator-5554
[2012-07-13 19:58:43 - AndroidSelectImage] Waiting for HOME ('android.process.acore') to be launched...

Android Er said...

hello lse123,

I don't know your exact case, but according to your log, it seem that the emulator not yet started!

And I found that the code cannot display large image on ImageView, refer to the post: Scale bitmap Efficiently.