在gridview中获取文件夹图像

时间:2012-07-06 05:30:04

标签: android gridview sd-card

从sdcard文件夹获取图像并在gridview中查看时出现错误。

我的班级如下:

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;
        }

    }

}

我的xml文件如下:

<?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>

logcat错误显示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)

我不知道如何解决这个问题。请提前帮助我

1 个答案:

答案 0 :(得分:0)

  1. 您使用的imagenames数组大小为100,但您不知道“WallpaperPro”文件夹中存在多少张图片。因此,您必须使用ArrayList

  2. 您的GridView大小也是100,但图像名称数组可能包含少于100个图像。这是空指针异常的主要原因。

  3. 将此行String backupPath=Environment.getExternalStorageDirectory().getPath() + "/WallpaperPro";用作String backupPath=Environment.getExternalStorageDirectory() + "/WallpaperPro";String backupPath=Environment.getExternalStorageDirectory().getAbsolutePath() + "/WallpaperPro";

  4. 确保您的文件夹名称为WallpaperPro

  5. 我希望这会对您有所帮助,对任何查询发表评论。