Read all Images from Assets Folder and Show in GridView Android Java

Get all images (Bitmap) from android assets folder and show them in GridView in Android Java Tutorial Example

 

gridview screenshot
Android gridview example

 

Here is the java code for android to show all photos,bitmaps or images from the assets folder into gridview in android java, these kind of codes  are used for android apps such as photo gallery, wallpaper gallery etc.

You can use this code to make your own gallery or wallpaper app, see the screenshot of this code above deployed to an app

This code shows all the images stored in the android assets folder and then display them inside the gridview, you can add more code in this project, for example – to show the selected image inside a full image view.

 

So let’s start

Create a New Android Project

Create a GridView in the Activity_main.xml

 

<GridView
    android:id="@+id/gridview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:columnWidth="90dp"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center"/>

 

 

Now, Go to MainActivity.java, and enter the code below

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Adapter;
import android.widget.GridView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    GridView gridView;

    Adapter adapter;


    ArrayList<String> listPath;


    //Bitmap listBitmap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    gridView  =  findViewById(R.id.gridview);
    ImageAdapter gridAdapter =(new ImageAdapter(this));
        gridView.setAdapter(gridAdapter);




    }}

 

Now we need create an ImageAdapter for the grid view, the adapter gets the images and fills the images inside grids.




To create a new ImageAdapter Java class, right click on your project folder, select New > Java Class, Give it the name ImageAdapter

 

Now Copy this code inside the ImageAdapter.java class

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.io.IOException;
import java.io.InputStream;




public class ImageAdapter extends BaseAdapter {
    private Context context;
    private String[] list;

    public ImageAdapter(Context c) {
        context = c;
        try {
            list = context.getAssets().list("imgs");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public int getCount() {
        return list.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView img;

        if (convertView == null) {

            img = new ImageView(context);
            LinearLayout.LayoutParams params = new      LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,   ViewGroup.LayoutParams.WRAP_CONTENT);
            img.setLayoutParams(new GridView.LayoutParams(params));
            img.setScaleType(ImageView.ScaleType.CENTER);
            img.setPadding(8, 8, 8, 8);

        } else {

            img = (ImageView) convertView;
        }
        try {

            InputStream ims = context.getAssets().open("imgs/" + list[position]);

            //Drawable d = Drawable.createFromStream(ims, null);
            Bitmap bitmap = BitmapFactory.decodeStream(ims);
            img.setImageBitmap(bitmap);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return img;
    }}

 

Now create the assets folder and inside it create another folder, let’s give it the name “imgs“.

Now put (Copy paste) all the images to the imgs folder, that is it. you can now run the app and test if everything works, if you having any problem, drop a comment below

 

, , , , ,

Post navigation

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.