如何使用毕加索库将sdcard中的多个图像加载到gridview中?

时间:2014-11-14 04:41:18

标签: android gridview picasso

我需要从SD卡加载图像,我必须在gridview中显示这些图像。如果有人通过代码知道,请帮助我。

这是我的mainActivity类

gv=(GridView)findViewById(R.id.grid);

    pd=new PicasaAdapter(getApplicationContext(), filePathList);

    String[] projection = { MediaStore.Files.FileColumns._ID,
            MediaStore.Files.FileColumns.DATA,
            MediaStore.Files.FileColumns.DATE_ADDED,
            MediaStore.Files.FileColumns.MEDIA_TYPE,
            MediaStore.Files.FileColumns.MIME_TYPE,
            MediaStore.Files.FileColumns.TITLE };

    selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "="
            + MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE ;

    Uri queryUri = MediaStore.Files.getContentUri("external");

    imgcursor = getContentResolver().query(queryUri, projection, selection,
            null, MediaStore.Files.FileColumns.DATE_ADDED + " DESC");

    Log.d(tag, "Image Cursor :" + imgcursor);
    int Totalcount = imgcursor.getCount();
    Log.d(tag, "Total Count :" + Totalcount);

    if (imgcursor != null) {

        Fetcher fet = new Fetcher(MainActivity.this, imgcursor);
        fet.execute(OFFSET);
    }


    gv.setAdapter(pd);


}

这是我的自定义适配器类

public class PicasaAdapter extends BaseAdapter {

private Context mContext;
private ArrayList<String>fileUri = new ArrayList<String>();
private String tag = "Picaso Adapter";
private LayoutInflater mInflater;

public PicasaAdapter(Context mContext, ArrayList<String> filePathList) {
    // TODO Auto-generated constructor stub
    this.mContext = mContext;
    fileUri = filePathList;
    mInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    Log.d(tag, "Total Path :"+fileUri.size());
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return 0;
}

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    if (convertView == null) {

        convertView = mInflater.inflate(
                R.layout.image, null);
    }
    ImageView imageView = (ImageView)convertView.findViewById(R.id.imageView);

    Log.d(tag, "Getting Path to Load: "+fileUri.get(position));
    Picasso.with(mContext).load(fileUri.get(position)).into(imageView);

    return convertView;
}

这是Fetcher类

public class Fetcher extends AsyncTask<Integer, Integer, Integer> {

ProgressDialog mProgressDialog;
String fMime_type;
Context mContext;
int i, count, j, id, dataColumnIndex;
private Cursor imgcursor;
String TAG = "Fetcher";
@SuppressLint("NewApi")
MediaMetadataRetriever mRetriever = new MediaMetadataRetriever();

public ArrayList<String> arrPathList;
ArrayList<String> mimeType = new ArrayList<String>();

public Fetcher(Context context, Cursor cur) {
    // TODO Auto-generated constructor stub
    mContext = context;
    arrPathList = new ArrayList<String>();
    imgcursor = cur; 

}

@SuppressLint("NewApi")
@Override
protected Integer doInBackground(Integer... params) {
    // TODO Auto-generated method stub

    if (params[0] != null)
        count = params[0];

    if (count == 15)
        j = 0;

    for (int i = j; i < count; i++) {

        imgcursor.moveToPosition(i);
        // Getting Mime-type of the file
        String mime_Type = imgcursor.getString(imgcursor
                .getColumnIndex(MediaStore.MediaColumns.MIME_TYPE));
        Log.d(TAG, "Mime_Type : " + mime_Type);
        if (mime_Type.equals("jpg") || mime_Type.equals("webp")
                || mime_Type.equals("png") || mime_Type.equals("bmp")
                || mime_Type.equals("gif")) {
            fMime_type = "image";
        } else if (mime_Type.equals("mp4") || mime_Type.equals("3gp")
                || mime_Type.equals("mkv")) {
            fMime_type = "video";
        } else
            fMime_type = mime_Type.substring(0, 5);

        if (fMime_type.equals("image")) {

            id = imgcursor.getInt(imgcursor
                    .getColumnIndex(MediaStore.Images.Media._ID));
            dataColumnIndex = imgcursor
                    .getColumnIndex(MediaStore.Images.Media.DATA);
            /*if (bitList != null)
                bitList.add(MediaStore.Images.Thumbnails.getThumbnail(
                        mContext.getContentResolver(), id,
                        MediaStore.Images.Thumbnails.MICRO_KIND, null));
            durationcount.add(null);*/
            mimeType.add(fMime_type);
            arrPathList.add(imgcursor.getString(dataColumnIndex));
            Log.d(TAG, "PathString: "+arrPathList.get(i));

        }
            File f = new File(path);

    j = count;

    return null;
}

@Override
protected void onPreExecute() {
    // TODO Auto-generated method stub
    super.onPreExecute();
    // isLoading = true;

    try {
        mProgressDialog = new ProgressDialog(mContext);
        mProgressDialog.setMessage("Loading...");
        if (mContext != null && !mProgressDialog.isShowing())
            mProgressDialog.show();

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

@Override
protected void onPostExecute(Integer result) {
    // TODO Auto-generated method stub
    super.onPostExecute(result);

    if(mProgressDialog != null && mProgressDialog.isShowing()){
        mProgressDialog.dismiss();
        mProgressDialog = null;
    }


    GridView gridView = (GridView)((Activity)mContext).findViewById(R.id.grid);
    // Instance of ImageAdapter Class
    gridView.setAdapter(new PicasaAdapter(mContext, arrPathList));
}

LogCat错误:

11-14 12:25:19.812: E/AndroidRuntime(805): FATAL EXCEPTION: main
11-14 12:25:19.812: E/AndroidRuntime(805): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.samplepicasa/com.example.samplepicasa.MainActivity}: java.lang.NullPointerException
11-14 12:25:19.812: E/AndroidRuntime(805):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
11-14 12:25:19.812: E/AndroidRuntime(805):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
11-14 12:25:19.812: E/AndroidRuntime(805):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
11-14 12:25:19.812: E/AndroidRuntime(805):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
11-14 12:25:19.812: E/AndroidRuntime(805):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-14 12:25:19.812: E/AndroidRuntime(805):  at android.os.Looper.loop(Looper.java:137)
11-14 12:25:19.812: E/AndroidRuntime(805):  at android.app.ActivityThread.main(ActivityThread.java:4424)
11-14 12:25:19.812: E/AndroidRuntime(805):  at java.lang.reflect.Method.invokeNative(Native Method)
11-14 12:25:19.812: E/AndroidRuntime(805):  at java.lang.reflect.Method.invoke(Method.java:511)
11-14 12:25:19.812: E/AndroidRuntime(805):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-14 12:25:19.812: E/AndroidRuntime(805):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-14 12:25:19.812: E/AndroidRuntime(805):  at dalvik.system.NativeStart.main(Native Method)
11-14 12:25:19.812: E/AndroidRuntime(805): Caused by: java.lang.NullPointerException
11-14 12:25:19.812: E/AndroidRuntime(805):  at com.example.samplepicasa.MainActivity.onCreate(MainActivity.java:50)
11-14 12:25:19.812: E/AndroidRuntime(805):  at android.app.Activity.performCreate(Activity.java:4465)
11-14 12:25:19.812: E/AndroidRuntime(805):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
11-14 12:25:19.812: E/AndroidRuntime(805):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
11-14 12:25:19.812: E/AndroidRuntime(805):  ... 11 more

谢谢。

1 个答案:

答案 0 :(得分:0)

getCount() - 此适配器表示的数据集中有多少项。您正在返回0表示您拥有该适配器的0个项目。像这样改变

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return fileUri.size();
}

getItem(int) - 获取与数据集中指定位置关联的数据项。像这样改变

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

getItemId(int) - 获取与列表中指定位置关联的行ID。像这样改变

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