Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8188845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:03:40+00:00 2026-06-07T03:03:40+00:00

Am getting error when getting images from sdcard folder and view in gridview. My

  • 0

Am getting error when getting images from sdcard folder and view in gridview.

My class as follows:

public class Favourites extends Activity{
    String backupPath=Environment.getExternalStorageDirectory().getPath() + "/WallpaperPro";
    String name;
    GridView favgrid;

    Cursor cursor;
    int columnIndex; 
    String[] imagenames=new String[100];

    @Override 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try{
         readfile();
        }catch(Exception e)
        {

        }
        setContentView(R.layout.fav);
        favgrid=(GridView) findViewById(R.id.favgrid);

        String[] projection = {MediaStore.Images.Media._ID};
        // Create the cursor pointing to the SDCard
        cursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                projection, 
              null ,
                null,  
                null);
        // Get the column index of the image ID
        Toast.makeText(getApplicationContext(),""+cursor.getCount() , 1).show();
        columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
        favgrid.setAdapter(new FavAdapter(this));


    }
    public void readfile()
    {
        //File sdCardRoot = Environment.getExternalStorageDirectory();
        File yourDir = new File(backupPath);
        int i=0;
        for (File f : yourDir.listFiles()) {
            if (f.isFile())
                 name = f.getName();
            imagenames[i]=backupPath+"/"+name;
            Log.i("CUTEWALLPAPER", "imagenames["+i+"]= "+name);
            i++;

                // make something with the name
        }
    }

    public class FavAdapter extends BaseAdapter {
        private Context mContext;
          public FavAdapter(Context c) { 
                mContext = c;
            }
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return imagenames.length;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }


        @Override
        public ImageView getView(int position, View convertView, ViewGroup parent) {
            // ImageView i = new ImageView(mContext);
             ImageView imageView;
                imageView = new ImageView(mContext);
                if (convertView == null) {

                    imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
                    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

                    imageView.setPadding(0, 0, 0, 0);
              } else {
                    imageView = (ImageView) convertView;
                }

             Bitmap b=BitmapFactory.decodeFile(imagenames[position].toString());
             Log.i("CUTEWALLPAPER", "Position"+position+" "+b.getHeight());
             imageView.setImageBitmap(b);

             imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); 
                /* Set the Width/Height of the ImageView. */ 
                imageView.setLayoutParams(new GridView.LayoutParams(300, 300));
                return imageView;
        }

    }

}

And my xml file as follows:

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent" android:weightSum="1" android:orientation="horizontal" android:layout_height="wrap_content"> 
     <GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/favgrid"
    android:layout_width="fill_parent"
    android:columnWidth="90dp"
    android:numColumns="1"
    android:verticalSpacing="0dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center" android:layout_height="fill_parent"/>
    </RelativeLayout>

The logcat error shows Null Pointer Exception.

07-06 22:57:26.892: ERROR/AndroidRuntime(302): java.lang.NullPointerException
07-06 22:57:26.892: ERROR/AndroidRuntime(302):     at com.cutewallpaper.Favourites$FavAdapter.getView(Favourites.java:112)
07-06 22:57:26.892: ERROR/AndroidRuntime(302):     at com.cutewallpaper.Favourites$FavAdapter.getView(Favourites.java:1)
07-06 22:57:26.892: ERROR/AndroidRuntime(302):     at android.widget.AbsListView.obtainView(AbsListView.java:1274)
07-06 22:57:26.892: ERROR/AndroidRuntime(302):     at android.widget.GridView.makeAndAddView(GridView.java:1218)
07-06 22:57:26.892: ERROR/AndroidRuntime(302):     at android.widget.GridView.makeRow(GridView.java:265)
07-06 22:57:26.892: ERROR/AndroidRuntime(302):     at android.widget.GridView.fillDown(GridView.java:218)
07-06 22:57:26.892: ERROR/AndroidRuntime(302):     at android.widget.GridView.fillFromTop(GridView.java:337)
07-06 22:57:26.892: ERROR/AndroidRuntime(302):     at android.widget.GridView.layoutChildren(GridView.java:1112)
07-06 22:57:26.892: ERROR/AndroidRuntime(302):     at android.widget.AbsListView.onLayout(AbsListView.java:1113)
07-06 22:57:26.892: ERROR/AndroidRuntime(302):     at android.view.View.layout(View.java:6830)
07-06 22:57:26.892: ERROR/AndroidRuntime(302):     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:900)
07-06 22:57:26.892: ERROR/AndroidRuntime(302):     at android.view.View.layout(View.java:6830)
07-06 22:57:26.892: ERROR/AndroidRuntime(302):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
07-06 22:57:26.892: ERROR/AndroidRuntime(302):     at android.view.View.layout(View.java:6830)
07-06 22:57:26.892: ERROR/AndroidRuntime(302):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
07-06 22:57:26.892: ERROR/AndroidRuntime(302):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
07-06 22:57:26.892: ERROR/AndroidRuntime(302):     at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
07-06 22:57:26.892: ERROR/AndroidRuntime(302):     at android.view.View.layout(View.java:6830)
07-06 22:57:26.892: ERROR/AndroidRuntime(302):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
07-06 22:57:26.892: ERROR/AndroidRuntime(302):     at android.view.View.layout(View.java:6830)
07-06 22:57:26.892: ERROR/AndroidRuntime(302):     at android.view.ViewRoot.performTraversals(ViewRoot.java:996)
07-06 22:57:26.892: ERROR/AndroidRuntime(302):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
07-06 22:57:26.892: ERROR/AndroidRuntime(302):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-06 22:57:26.892: ERROR/AndroidRuntime(302):     at android.os.Looper.loop(Looper.java:123)
07-06 22:57:26.892: ERROR/AndroidRuntime(302):     at android.app.ActivityThread.main(ActivityThread.java:4363)
07-06 22:57:26.892: ERROR/AndroidRuntime(302):     at java.lang.reflect.Method.invokeNative(Native Method)
07-06 22:57:26.892: ERROR/AndroidRuntime(302):     at java.lang.reflect.Method.invoke(Method.java:521)
07-06 22:57:26.892: ERROR/AndroidRuntime(302):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-06 22:57:26.892: ERROR/AndroidRuntime(302):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-06 22:57:26.892: ERROR/AndroidRuntime(302):     at dalvik.system.NativeStart.main(Native Method)

I didnt know how to solve this. Help me thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T03:03:41+00:00Added an answer on June 7, 2026 at 3:03 am
    1. you are using imagenames array with 100 size but you don’t know that how many images exist into the “WallpaperPro” folder. So you must need to use ArrayList.

    2. And your GridView size is also 100, but might possible that imagenames array contains less than 100 images. This is the main reason for null pointer exception.

    3. use this line String backupPath=Environment.getExternalStorageDirectory().getPath() + "/WallpaperPro"; as String backupPath=Environment.getExternalStorageDirectory() + "/WallpaperPro"; or String backupPath=Environment.getExternalStorageDirectory().getAbsolutePath() + "/WallpaperPro";

    4. make sure your folder name is WallpaperPro.

    I hope this will help you, comment for any query.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Update getting error when i try to call the method from Activity: SQLiteDatabase db
I am getting error ERROR/AndroidRuntime(1482): java.lang.NullPointerException from my ImageAdapter class.Please Help me to solve
im Displaying images from specific folder on sd Card in GridView till know all
I am getting an internal server error when I am attempting to upload images
Getting error while inserting values into database (SQL Server 2008) Implicit conversion from data
I am trying to save images in Django and getting the error The ImageOps
I need to grab a some images from a folder on a users computer
def embedd_image(): from google.appengine.api import images img1 = images.Image('/home/ubuntu/a.png') // Here I am getting
I've got a weird problem here: I'm getting a 500 error code from Apache
When remotely updating a Mercurial Repository, I am getting the following error from the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.