Introduction

Android is one of the most popular operating systems for mobiles. We use the camera Application to take images in our day to day lives. This module is used in a loT of photo editing applications. Thus, I will show you how to use the gallery images in an Android Application, using Android Studio. Android is a kernel-based operating system. It allows the user to modify the GUI components and the source code.
Requirements

Steps to be followed

Step 1
Open an Android Studio and start the new project.
Android
Step 2
Put the Application name and the company domain. If you wish to use C++ to code the project, include C++ support, followed by clicking Next.
Android
Step 3
Select Android minimum SDK. After you chose the minimum SDK, it will show the approximate percentage of the people who use that SDK, followed by clicking Next.
Android
Step 4
Choose the basic activity, followed by clicking Next.
Android
Step 5
Put the activity name and the layout name. Android Studio basically takes Java class name as what you provide for the activity name and click Finish.
Android
Step 6
First, you need to design the Application. Go to activity_select_image.xml, followed by clicking the text bottom. This XML file contains the designing code for an Android app. In the activity_select_image.xml, copy and paste the code given below.
activity_select_image.xml code
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. tools:context=".SelectImageActivity" >
  6. <Button
  7. android:id="@+id/button"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:layout_alignParentLeft="true"
  11. android:layout_alignParentTop="true"
  12. android:layout_marginLeft="15dp"
  13. android:layout_marginTop="15dp"
  14. android:text="@string/pick_button" />
  15. <ImageView
  16. android:id="@+id/image"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:layout_centerHorizontal="true"
  20. android:layout_centerVertical="true"
  21. android:src="@drawable/ic_launcher" />
  22. </RelativeLayout>
Step 7
In the SelectImageActivity.java, copy and paste the code given below. Java programming is the backend language for Android. Do not replace your package name, else an app will not run.
SelectImageActivity.java code
  1. package ganeshannt.showimages;
  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.view.View.OnClickListener;
  11. import android.widget.Button;
  12. import android.widget.ImageView;
  13. public class SelectImageActivity extends Activity implements OnClickListener {
  14. // Image loading result to pass to startActivityForResult method.
  15. private static int LOAD_IMAGE_RESULTS = 1;
  16. // GUI components
  17. private Button button; // The button
  18. private ImageView image;// ImageView
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_select_image);
  23. // Find references to the GUI objects
  24. button = (Button)findViewById(R.id.button);
  25. image = (ImageView)findViewById(R.id.image);
  26. // Set button's onClick listener object.
  27. button.setOnClickListener(this);
  28. }
  29. @Override
  30. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  31. super.onActivityResult(requestCode, resultCode, data);
  32. // Here we need to check if the activity that was triggers was the Image Gallery.
  33. // If it is the requestCode will match the LOAD_IMAGE_RESULTS value.
  34. // If the resultCode is RESULT_OK and there is some data we know that an image was picked.
  35. if (requestCode == LOAD_IMAGE_RESULTS && resultCode == RESULT_OK && data != null) {
  36. // Let's read picked image data - its URI
  37. Uri pickedImage = data.getData();
  38. // Let's read picked image path using content resolver
  39. String[] filePath = { MediaStore.Images.Media.DATA };
  40. Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
  41. cursor.moveToFirst();
  42. String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));
  43. // Now we need to set the GUI ImageView data with data read from the picked file.
  44. image.setImageBitmap(BitmapFactory.decodeFile(imagePath));
  45. // At the end remember to close the cursor or you will end with the RuntimeException!
  46. cursor.close();
  47. }
  48. }
  49. @Override
  50. public void onClick(View v) {
  51. // Create the Intent for Image Gallery.
  52. Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  53. // Start new activity with the LOAD_IMAGE_RESULTS to handle back the results when image is picked from the Image Gallery.
  54. startActivityForResult(i, LOAD_IMAGE_RESULTS);
  55. }
  56. }
Step 8
In AndroidManifest.xml, add the code given below.
AndroidManifest.xml code
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="ganeshannt.showimages"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6. <uses-sdk
  7. android:minSdkVersion="19"
  8. android:targetSdkVersion="25" />
  9. <application
  10. android:allowBackup="true"
  11. android:icon="@drawable/ic_launcher"
  12. android:label="@string/app_name"
  13. android:theme="@style/AppTheme" >
  14. <activity
  15. android:name="ganeshannt.showimages.SelectImageActivity"
  16. android:label="@string/app_name" >
  17. <intent-filter>
  18. <action android:name="android.intent.action.MAIN" />
  19. <category android:name="android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </activity>
  22. </application>
  23. </manifest>
Step 9
In the Strings.xml, add the code given below. In this string, provide the app settings bar.
Strings.xml code
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string name="app_name">Show Images</string>
  4. <string name="menu_settings">Settings</string>
  5. <string name="pick_button">Pick image</string>
  6. </resources>
Step 13
This is our user interface of the Application. Click Make project option.
Android
Step 14
Run the Application, followed by choosing the virtual machine. Click OK.
Android
Deliverables
Here, we have successfully used the gallery images in an Android Application, using an Android Studio, which is created and executed.
Android
Click the pick image button and it will open the gallery.
Android
Choose the image, followed by just touching the image.
Android
If you have any doubts, just comment.