遍历图片文件,返回目录集合

使用Android自身的LoaderManager

public class MediaStoreHelper {
  //所有图片的目录放在第一个位置
  public final static int INDEX_ALL_PHOTOS = 0;
  //是否查找gif图片
  public final static String EXTRA_SHOW_GIF = "SHOW_GIF";

  public static void getPhotoDirs(FragmentActivity activity, Bundle args, PhotosResultCallback resultCallback) {
    activity.getSupportLoaderManager().initLoader(0, args, new PhotoDirLoaderCallbacks(activity, resultCallback));
  }
  //LoaderManager的回调
  static class PhotoDirLoaderCallbacks implements LoaderManager.LoaderCallbacks<Cursor> {

    private WeakReference<Context> context;
    private PhotosResultCallback resultCallback;

    public PhotoDirLoaderCallbacks(Context context, PhotosResultCallback resultCallback) {
      this.context = new WeakReference<>(context);
      this.resultCallback = resultCallback;
    }

    @Override public Loader<Cursor> onCreateLoader(int id, Bundle args) {
      return new PhotoDirectoryLoader(context.get(), args.getBoolean(EXTRA_SHOW_GIF, false));
    }

    @Override public void onLoadFinished(Loader<Cursor> loader, Cursor data) {

      if (data == null)  return;
      List<PhotoDirectory> directories = new ArrayList<>();
      PhotoDirectory photoDirectoryAll = new PhotoDirectory();
      photoDirectoryAll.setName(context.get().getString(R.string.publish_all_photo));
      photoDirectoryAll.setId("ALL");

      while (data.moveToNext()) {
        //图片id
        int imageId  = data.getInt(data.getColumnIndexOrThrow(_ID));
        //目录id
        String bucketId = data.getString(data.getColumnIndexOrThrow(BUCKET_ID));
        //目录名称
        String name = data.getString(data.getColumnIndexOrThrow(BUCKET_DISPLAY_NAME));
        String path = data.getString(data.getColumnIndexOrThrow(DATA));

        PhotoDirectory photoDirectory = new PhotoDirectory();
        photoDirectory.setId(bucketId);
        photoDirectory.setName(name);

        if (!directories.contains(photoDirectory)) {
          photoDirectory.setCoverPath(path);
          photoDirectory.addPhoto(imageId, path);
          photoDirectory.setDateAdded(data.getLong(data.getColumnIndexOrThrow(DATE_ADDED)));
          directories.add(photoDirectory);
        } else {
          directories.get(directories.indexOf(photoDirectory)).addPhoto(imageId, path);
        }

        photoDirectoryAll.addPhoto(imageId, path);
      }
      if (photoDirectoryAll.getPhotoPaths().size() > 0) {
        photoDirectoryAll.setCoverPath(photoDirectoryAll.getPhotoPaths().get(0));
      }
      directories.add(INDEX_ALL_PHOTOS, photoDirectoryAll);
      if (resultCallback != null) {
        resultCallback.onResultCallback(directories);
      }
    }

    @Override public void onLoaderReset(Loader<Cursor> loader) {

    }
  }


  public interface PhotosResultCallback {
    void onResultCallback(List<PhotoDirectory> directories);
  }

}

具体的查询参数设置

public class PhotoDirectoryLoader extends CursorLoader {

  final String[] IMAGE_PROJECTION = {
      Media._ID,
      Media.DATA,
      Media.BUCKET_ID,
      Media.BUCKET_DISPLAY_NAME,
      Media.DATE_ADDED
  };

  public PhotoDirectoryLoader(Context context, boolean showGif) {
    super(context);

    setProjection(IMAGE_PROJECTION);
    setUri(Media.EXTERNAL_CONTENT_URI);
    setSortOrder(Media.DATE_ADDED + " DESC");

    setSelection(
        MIME_TYPE + "=? or " + MIME_TYPE + "=? or "+ MIME_TYPE + "=? " + (showGif ? ("or " + MIME_TYPE + "=?") : ""));
    String[] selectionArgs;
    if (showGif) {
      selectionArgs = new String[] { "image/jpeg", "image/png", "image/jpg","image/gif" };
    } else {
      selectionArgs = new String[] { "image/jpeg", "image/png", "image/jpg" };
    }
    setSelectionArgs(selectionArgs);
  }

}

调用方式

        mDirectories = new ArrayList<>();
        Bundle mediaStoreArgs = new Bundle();
        mediaStoreArgs.putBoolean(MediaStoreHelper.EXTRA_SHOW_GIF, false);
        MediaStoreHelper.getPhotoDirs(this, mediaStoreArgs, new MediaStoreHelper.PhotosResultCallback() {
            @Override public void onResultCallback(List<PhotoDirectory> dirs) {
                mDirectories.clear();
                mDirectories.addAll(dirs);
                //等目录初始化结束后再添加选中图片所在的目录
                for (int i = 0; i < selectPhotoList.size(); i++) {
                    addSelectDirectories(selectPhotoList.get(i));
                }
            }
        });

目录对象比较是否为同一个对象

void CreateGraph Graph graph 图的创建图 { ENode p q e; int i; cout<<"请输入连通无向图的顶点数和边数 例如 3 3: n"; scanf "%d %d" &graph >numberOfVerts &graph >numberOfEerts ; for i 1;i< graph >numberOfVerts;i++ { cout<<"请输入第"<<i<<"个顶点的信息: n"; cin>>graph >amlist [i] data; graph >amlist [i] number i; graph >amlist[i] firstedge NULL; graph >amlist [i] mark 0; } for i 1;i< graph >numberOfEerts;i++ { p ENode malloc sizeof ENode ; cout<<"请输入每条边的信息(编号小的在前 例如1 3回车1 2回车2 3) n"; cin>>p >ivex>>p >jvex; p >ilink p >jlink NULL; if graph >amlist [p >ivex ] firstedge NULL graph >amlist [p >ivex ] firstedge p; else { q graph >amlist [p >ivex ] firstedge ; while q NULL { e q; if q >ivex p >ivex q q >ilink ; else q q >jlink ; } if e >ivex p >ivex e >ilink p; else e >jlink p; } if graph >amlist [p >jvex ] firstedge NULL graph >amlist [p >jvex ] firstedge p; else { q graph >amlist [p >jvex ] firstedge ; while q NULL { e q; if q >ivex p >ivex q q >ilink ; else q q >jlink ; } if e >ivex p >ivex e >ilink p; else e >jlink p; } } } void SetMark Graph graph 设置访问标记 { int i; for i 1;i< graph >numberOfVerts ;i++ graph >amlist [i] mark 0; } void DFS Graph graph int v 深度遍历 { ENode p; cout<<v<<" "; graph >amlist [v] mark 1; p graph >amlist [v] firstedge ; while p NULL { if p >ivex v { if graph >amlist [p >jvex ] mark 0 { cout<<"<"<<p >ivex<<" "<<p >jvex<<">"<<endl; DFS graph p >jvex ; } p p >ilink ; } else { if graph >amlist [p >ivex] mark 0 { cout<<"<"<<p >jvex<<" "<<p >ivex<<">"<<endl; DFS graph p >ivex ; } p p >jlink ; } } }">void CreateGraph Graph graph 图的创建图 { ENode p q e; int i; cout<<"请输入连通无向图的顶点数和边数 例如 3 3: n"; scanf "%d %d" &graph >numberOfVerts &graph >numberOfEerts ; for i 1;i< graph >numberOfVerts;i++ { cou [更多]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值