Translate this page

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

 

Related Posts

person using macbook pro on person s lap

Fix android.content.Intent.migrateExtraStreamToClipData(android.content.Context)’ on a null object reference

Here is how to Fix Android Studio Error Attempt to invoke virtual method ‘boolean android.content.Intent.migrateExtraStreamToClipData(android.content.Context)’ on a null object reference This error is caused by a number…

Android Spinner onItemSelected Listener Example

Spinner is an android UI element; it provides a drop-down List where you can choose an item from the list. You will need a listener to catch…

person in gray shirt holding a small paper with texts

Get Current Month Name in Java Android

The code below can be used to get the name of current month, for example, January, February, March etc. Just call this method and it will return…

The future of android developers is not good

Are you an android developer or looking to be an android developer? You should read this Myself is zid, I’m an android developer and been in this…

How to Auto Scroll RecyclerView Android Java

If you want to scroll your recyclerview automatically, you can use the below code. Just add this code after setting your recyclerview adapter

Download Android Earning App Source Code

Android Earning App Source Code Download for Android studio 2023 This is cashJungle earning app developed by us. It has amazing jungle theme as its name suggests….

This Post Has 5 Comments

Leave a Reply

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