Open Gallery and get the selected image in appplication

In this, the screen will have a button to open the android device Gallery and from there if we select any image, that image we can get it in the application.

The code snipped for opening the Gallery is,


Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 1);


We are opening the gallery as a sub activity of our application, because onActivityResult() will get called after selecting the image from gallery. So in onACtivityResult(), we will get the Uri of the selected image and from Mediastore contentprovider, we can get the selected image as bitmap. After that, for showing it in the application, just setting the image in a ImageView.

The complete code is,


public class GalWalActivity extends Activity {

private ImageView selectedImage;
private Bitmap currentImage;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
selectedImage = (ImageView) findViewById(R.id.selectedImage);
Button openGallery = (Button) findViewById(R.id.opengallery);

openGallery.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 1);
}
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (resultCode == RESULT_OK) {
Uri photoUri = data.getData();
if (photoUri != null) {
try {
currentImage = MediaStore.Images.Media.getBitmap(this.getContentResolver(), photoUri);
selectedImage.setImageBitmap(currentImage);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}



The catch here is if we open the gallery for first time from the applicaiotn, the applicaiton will work fine. But if we open gallery for second time and try to get the selected image, we will get an exception OutOfMemory Exception

This is because we are not free the bitmap memory or we need to recycle the bitmap. The solution for this is, every time when we open gallery from our application, onPause() and onStop() of the Activity lifecycle methods will get called. So in either onPause() or onStop(), we need to add the code snippet to recycle the bitmap and to free the memory.


if (currentImage != null) {
currentImage.recycle();
currentImage = null;
System.gc();
}


So onStop() will look like this.


public void onStop() {
super.onStop();
if (currentImage != null) {
currentImage.recycle();
currentImage = null;
System.gc();
}
}


The layout will look like this,


<?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"
>
<Button android:id="@+id/opengallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open Gallery"
/>
<ImageView android:id="@+id/selectedImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>


Happy Coding

Comments

  1. Thanks for your valuable post which includes recycling bitmap too.

    ReplyDelete

Post a Comment

Popular posts from this blog

AlarmManager and Notification in Android

Swipe Action and ViewFlipper in Android