Introduction

In this article, I will tell you how to import an image from the Gallery in Android.
If we want to change the background in Android then we import an from the Gallery and set that image as the background image.
To do that we must use the following procedure.
Step 1
To create a new project: "New" --> "Android Application project" --> set the name to "Image_picker."
newimage.jpg
Step 2
Open the Image_picker/Manifest.xml file and update it using the following code.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.novoda"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6. <uses-permission android:name="android.permission.READ_CONTACTS" />
  7. <uses-sdk android:minSdkVersion="3" />
  8. <application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
  9. <activity android:name=".ContactSelector"
  10. android:label="@string/app_name">
  11. <intent-filter>
  12. <action android:name="android.intent.action.MAIN" />
  13. <category android:name="android.intent.category.LAUNCHER" />
  14. </intent-filter>
  15. </activity>
  16. </application>
  17. </manifest>
Step 3
Open the Image_picker/res/layout/activity_main.xml file and update it using the following code.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent">
  6. <ImageView
  7. android:id="@+id/imgView"
  8. android:layout_width="fill_parent"
  9. android:layout_weight="1" android:layout_height="wrap_content"></ImageView>
  10. <Button
  11. android:layout_height="wrap_content"
  12. android:text="Load Picture"
  13. android:layout_width="wrap_content"
  14. android:id="@+id/buttonLoadPicture"
  15. android:layout_weight="0"
  16. android:layout_gravity="center"></Button>
  17. </LinearLayout>
Step 4
Open the Image_picker/res/values/string.xml file and update it using the following code.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string name="hello">Hello World, ImageGalleryDemoActivity!</string>
  4. <string name="app_name">ImageGalleryDemo</string>
  5. </resources>
Step 5
Open Image_picker/src/com.image.picker/MainActivity.java and update this file using your contact picking logic as in the following.
  1. package net.viralpatel.android.imagegalleray;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.database.Cursor;
  5. import android.graphics.BitmapFactory;
  6. import android.net.Uri;
  7. import android.os.Bundle;
  8. import android.provider.MediaStore;
  9. import android.view.View;
  10. import android.widget.Button;
  11. import android.widget.ImageView;
  12. public class MainActivity extends Activity {
  13. private static int RESULT_LOAD_IMAGE = 1;
  14. @Override
  15. public void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.main);
  18. Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
  19. buttonLoadImage.setOnClickListener(new View.OnClickListener() {
  20. @Override
  21. public void onClick(View arg0) {
  22. Intent i = new Intent(
  23. Intent.ACTION_PICK,
  24. android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  25. startActivityForResult(i, RESULT_LOAD_IMAGE);
  26. }
  27. });
  28. }
  29. @Override
  30. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  31. super.onActivityResult(requestCode, resultCode, data);
  32. if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
  33. Uri selectedImage = data.getData();
  34. String[] filePathColumn = { MediaStore.Images.Media.DATA };
  35. Cursor cursor = getContentResolver().query(selectedImage,
  36. filePathColumn, null, null, null);
  37. cursor.moveToFirst();
  38. int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
  39. String picturePath = cursor.getString(columnIndex);
  40. cursor.close();
  41. ImageView imageView = (ImageView) findViewById(R.id.imgView);
  42. imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
  43. }
  44. }
  45. }
Step 6
Run the application in an Android virtual device and see the output.
Load an Image
imgloaader.jpg
Select an Image
choose.jpg
Loaded Image
loaded.jpg