How To Use The Gallery Images In Android Application

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.   
  7.     <Button  
  8.         android:id="@+id/button"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentLeft="true"  
  12.         android:layout_alignParentTop="true"  
  13.         android:layout_marginLeft="15dp"  
  14.         android:layout_marginTop="15dp"  
  15.         android:text="@string/pick_button" />  
  16.   
  17.     <ImageView  
  18.         android:id="@+id/image"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:layout_centerHorizontal="true"  
  22.         android:layout_centerVertical="true"  
  23.         android:src="@drawable/ic_launcher" />  
  24.   
  25. </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.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.database.Cursor;  
  6. import android.graphics.BitmapFactory;  
  7. import android.net.Uri;  
  8. import android.os.Bundle;  
  9. import android.provider.MediaStore;  
  10. import android.view.View;  
  11. import android.view.View.OnClickListener;  
  12. import android.widget.Button;  
  13. import android.widget.ImageView;  
  14.   
  15.   
  16. public class SelectImageActivity extends Activity implements OnClickListener {  
  17.   
  18.     // Image loading result to pass to startActivityForResult method.  
  19.     private static int LOAD_IMAGE_RESULTS = 1;  
  20.   
  21.     // GUI components  
  22.     private Button button; // The button  
  23.     private ImageView image;// ImageView  
  24.   
  25.     @Override  
  26.     protected void onCreate(Bundle savedInstanceState) {  
  27.         super.onCreate(savedInstanceState);  
  28.         setContentView(R.layout.activity_select_image);  
  29.   
  30.         // Find references to the GUI objects  
  31.         button = (Button)findViewById(R.id.button);  
  32.         image = (ImageView)findViewById(R.id.image);  
  33.   
  34.         // Set button's onClick listener object.  
  35.         button.setOnClickListener(this);  
  36.   
  37.     }  
  38.   
  39.     @Override  
  40.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  41.         super.onActivityResult(requestCode, resultCode, data);  
  42.   
  43.         // Here we need to check if the activity that was triggers was the Image Gallery.  
  44.         // If it is the requestCode will match the LOAD_IMAGE_RESULTS value.  
  45.         // If the resultCode is RESULT_OK and there is some data we know that an image was picked.  
  46.         if (requestCode == LOAD_IMAGE_RESULTS && resultCode == RESULT_OK && data != null) {  
  47.             // Let's read picked image data - its URI  
  48.             Uri pickedImage = data.getData();  
  49.             // Let's read picked image path using content resolver  
  50.             String[] filePath = { MediaStore.Images.Media.DATA };  
  51.             Cursor cursor = getContentResolver().query(pickedImage, filePath, nullnullnull);  
  52.             cursor.moveToFirst();  
  53.             String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));  
  54.   
  55.             // Now we need to set the GUI ImageView data with data read from the picked file.  
  56.             image.setImageBitmap(BitmapFactory.decodeFile(imagePath));  
  57.   
  58.             // At the end remember to close the cursor or you will end with the RuntimeException!  
  59.             cursor.close();  
  60.         }  
  61.     }  
  62.   
  63.     @Override  
  64.     public void onClick(View v) {  
  65.         // Create the Intent for Image Gallery.  
  66.         Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);  
  67.   
  68.         // Start new activity with the LOAD_IMAGE_RESULTS to handle back the results when image is picked from the Image Gallery.  
  69.         startActivityForResult(i, LOAD_IMAGE_RESULTS);  
  70.     }  
  71.   
  72. }  
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.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="19"  
  9.         android:targetSdkVersion="25" />  
  10.   
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name="ganeshannt.showimages.SelectImageActivity"  
  18.             android:label="@string/app_name" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.   
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.         </activity>  
  25.     </application>  
  26.   
  27. </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.   
  4.     <string name="app_name">Show Images</string>  
  5.     <string name="menu_settings">Settings</string>  
  6.     <string name="pick_button">Pick image</string>  
  7.   
  8. </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.


Similar Articles