1

我正在尝试开发一个应用程序,该应用程序在 SDCard 的文件夹中创建图像并显示它们(“MyCreatedImages”文件夹现在显示在图库中),现在我正在尝试从 gridview 布局中的给定文件夹加载所有图像。我正在使用以下代码,但是由于我无法理解的原因,我不断获得 NPE。

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.activity_my_image_fragment,
            container, false);

    // Set up an array of the Thumbnail Image ID column we want
    String[] projection = { MediaStore.Images.Thumbnails._ID };

    // Create the cursor pointing to the SDCard
    cursor = getActivity().getContentResolver().query(
            Uri.parse("file://" + Environment.getExternalStorageDirectory()+ File.separator + "MyCreatedImages"), projection,
            null, null, MediaStore.Images.Thumbnails._ID);

    // Get the column index of the Thumbnails Image ID
    columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);

    GridView gridview = (GridView) root.findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(this.getActivity()));
    //this.registerForContextMenu(gridview);
    return root;
}

还有我的 Gridview 适配器:

private class ImageAdapter extends BaseAdapter {
    private Context context;

    public ImageAdapter(Context localContext) {
        context = localContext;
    }

    public int getCount() {
        return cursor.getCount();
    }

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

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

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView picturesView;
        if (convertView == null) {
            picturesView = new ImageView(context);
            // Move cursor to current position
            cursor.moveToPosition(position);
            // Get the current value for the requested column
            int imageID = cursor.getInt(columnIndex);
            // Set the content of the image based on the provided URI
            picturesView.setImageURI(Uri.withAppendedPath(
                    MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
                    "" + imageID));
            picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            picturesView.setPadding(8, 8, 8, 8);
            picturesView
                    .setLayoutParams(new GridView.LayoutParams(100, 100));
        } else {
            picturesView = (ImageView) convertView;
        }
        return picturesView;
    }
}

打开此片段后,我收到以下 LogCat 错误:

09-23 16:25:06.286: E/AndroidRuntime(28497): FATAL EXCEPTION: main
09-23 16:25:06.286: E/AndroidRuntime(28497): java.lang.NullPointerException
09-23 16:25:06.286: E/AndroidRuntime(28497):    at com.meme.hdmeme.MyMemeFragment.onCreateView(MyMemeFragment.java:51)
09-23 16:25:06.286: E/AndroidRuntime(28497):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
09-23 16:25:06.286: E/AndroidRuntime(28497):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
09-23 16:25:06.286: E/AndroidRuntime(28497):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
09-23 16:25:06.286: E/AndroidRuntime(28497):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
09-23 16:25:06.286: E/AndroidRuntime(28497):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
09-23 16:25:06.286: E/AndroidRuntime(28497):    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
09-23 16:25:06.286: E/AndroidRuntime(28497):    at android.os.Handler.handleCallback(Handler.java:615)
09-23 16:25:06.286: E/AndroidRuntime(28497):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-23 16:25:06.286: E/AndroidRuntime(28497):    at android.os.Looper.loop(Looper.java:137)
09-23 16:25:06.286: E/AndroidRuntime(28497):    at android.app.ActivityThread.main(ActivityThread.java:4921)
09-23 16:25:06.286: E/AndroidRuntime(28497):    at java.lang.reflect.Method.invokeNative(Native Method)
09-23 16:25:06.286: E/AndroidRuntime(28497):    at java.lang.reflect.Method.invoke(Method.java:511)
09-23 16:25:06.286: E/AndroidRuntime(28497):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
09-23 16:25:06.286: E/AndroidRuntime(28497):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
09-23 16:25:06.286: E/AndroidRuntime(28497):    at dalvik.system.NativeStart.main(Native Method)

第 51 行是:

    columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
4

1 回答 1

4

试试这个函数从 sd 卡获取图像:

 ArrayList<String> f = new ArrayList<String>();   // list of available files in  path
  File[] listFile;

   public void getSdcardImages()
  {
File file= new File(android.os.Environment.getExternalStorageDirectory(),"MyFolder");

    if (file.isDirectory())
    {
        listFile = file.listFiles();


        for (int i = 0; i < listFile.length; i++)
        {

            f.add(listFile[i].getAbsolutePath());

        }
    }
 }

使用权限:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

然后通过适配器在网格视图中设置:

 GridView imggrid = (GridView) findViewById(R.id.imgGrid);
imgAdapter = new ImageAdapter();
imggrid .setAdapter(imgAdapter);

如下设置适配器:

  public class ImageAdapter extends BaseAdapter {
private LayoutInflater mInflater;

public ImageAdapter() {
    mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public int getCount() {
    return f.size();
}

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

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

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        holder = new ViewHolder();
        convertView = mInflater.inflate(
                R.layout.gallery, null);
        holder.imageview = (ImageView) convertView.findViewById(R.id.thumb);

        convertView.setTag(holder);
    }
    else {
        holder = (ViewHolder) convertView.getTag();
    }


    Bitmap myBitmap = BitmapFactory.decodeFile(f.get(position));
    holder.imageview.setImageBitmap(myBitmap);
    return convertView;
  }
}

class ViewHolder {
    ImageView imageview;


}
于 2013-09-23T15:42:06.143 回答