Hello Friends, Today I will share how to develop image gallery app in Android. Actually, recently I was developing an Android Application in which I was required to add inbuilt gallery feature for the captured images through that application. So, for learning purpose I developed separate gallery application and now I am sharing this code to help others. While developing gallery app I faced OutOfMemoryError (Thrown when the Java Virtual Machine cannot allocate an object because it is out of memory, and no more memory could be made available by the garbage collector.). This happened because lot of images loaded in memory and eventually the memory gets full, then I googled and found that, there is an existing library named Glide which is recommended by android itself to fetch, decode, and display bitmaps in your app. Glide does all the task related to bitmap images i.e loading, caching, memory management, etc. So, I used Glide library to overcome OutOfMemoryError. This gallery app will also work on android version M(marshmallow) and higher version.
To use Glide library we required to add below dependency in apps build.gradle file.
1 |
compile 'com.github.bumptech.glide:glide:3.7.0' |
So, Here is the code
AndroidManifest.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.zatackcoder.galleryapp"> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".ViewImage"></activity> </application> </manifest> |
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
package com.zatackcoder.galleryapp; import android.Manifest; import android.app.ActivityManager; import android.content.Intent; import android.content.pm.PackageManager; import android.os.Build; import android.os.Bundle; import android.os.Environment; import android.support.v4.app.ActivityCompat; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; import com.bumptech.glide.Glide; import java.io.File; import java.util.ArrayList; public class MainActivity extends AppCompatActivity implements ActivityCompat.OnRequestPermissionsResultCallback { GridView gridView; ArrayList<File> list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); checkPermissions(); list = getImageFiles(Environment.getExternalStorageDirectory()); gridView = (GridView) findViewById(R.id.gridView); gridView.setAdapter(new GridAdapter()); gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { startActivity(new Intent(getApplicationContext(), ViewImage.class).putExtra("img", list.get(i).toString())); } }); } class GridAdapter extends BaseAdapter { @Override public int getCount() { return list.size(); } @Override public Object getItem(int i) { return list.get(i); } @Override public long getItemId(int i) { return 0; } @Override public View getView(int i, View view, ViewGroup viewGroup) { view = getLayoutInflater().inflate(R.layout.grid_item, viewGroup, false); ImageView imageView = (ImageView) view.findViewById(R.id.imageView); //imageview.setImageURI(Uri.parse(getItem(i).toString())); //Loading image from below url into imageView Glide.with(getApplicationContext()) .load(getItem(i).toString()) .into(imageView); return view; } } ArrayList<File> getImageFiles(File root) { ArrayList<File> list = new ArrayList(); File[] files = root.listFiles(); for (int i = 0; i < files.length; i++) { if(files[i].isDirectory()) { list.addAll(getImageFiles(files[i])); } else { if(files[i].getName().endsWith(".jpg") || files[i].getName().endsWith(".png") || files[i].getName().endsWith(".gif")) { list.add(files[i]); } } } return list; } private void checkPermissions(){ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if(checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){ //Requesting permission. ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1); } } } @Override //Override from ActivityCompat.OnRequestPermissionsResultCallback Interface public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { switch (requestCode) { case 1: { // If request is cancelled, the result arrays are empty. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // permission granted } return; } } } private ActivityManager.MemoryInfo getMemoryInfo() { ActivityManager activityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE); ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo(); activityManager.getMemoryInfo(memoryInfo); return memoryInfo; } } |
ViewImage.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
package com.zatackcoder.galleryapp; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.ImageView; import com.bumptech.glide.Glide; public class ViewImage extends AppCompatActivity { ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_view_image); Intent intent = getIntent(); String file = intent.getStringExtra("img"); imageView = (ImageView) findViewById(R.id.imageView); //imageView.setImageURI(Uri.parse(file)); //Loading image from below url into imageView Glide.with(getApplicationContext()) .load(file) .into(imageView); } } |
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.zatackcoder.galleryapp.MainActivity"> <GridView android:id="@+id/gridView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="5dp" android:numColumns="auto_fit" android:columnWidth="80dp" android:verticalSpacing="5dp" android:horizontalSpacing="5dp" android:stretchMode="spacingWidth" android:layout_centerHorizontal="true"/> </RelativeLayout> |
grid_item.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/imageView" android:layout_width="80dp" android:layout_height="80dp" android:scaleType="centerCrop" app:srcCompat="@mipmap/ic_launcher" /> </LinearLayout> |
activity_view_image.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.zatackcoder.galleryapp.ViewImage"> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:scaleType="fitCenter" app:srcCompat="@mipmap/ic_launcher"/> </RelativeLayout> |
Screenshots
AndroidStudio Project Download
Image Gallery App Android Studio Project
Thanks for stopping by please share if you like it
Comments