android 获取手机中的所有图片或某一目录下的图片方法

2020-11-02 12:23:00 浏览数 (1)

获取手机中的所有图片,并过滤获取某一目录下的图片。(注释掉的代码可以按照目录分组)

代码语言:javascript复制
private void getAllPhotoInfo() {
new Thread(new Runnable() {
@Override
public void run() {
//        List<MediaBean  mediaBeen = new ArrayList< ();
HashMap<String,List<MediaBean   allPhotosTemp = new HashMap< ();//所有照片
Uri mImageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] projImage = { MediaStore.Images.Media._ID
, MediaStore.Images.Media.DATA
,MediaStore.Images.Media.SIZE
,MediaStore.Images.Media.DISPLAY_NAME};
final Cursor mCursor = getContentResolver().query(mImageUri,
projImage,
MediaStore.Images.Media.MIME_TYPE   "=? or "   MediaStore.Images.Media.MIME_TYPE   "=?",
new String[]{"image/jpeg", "image/png"},
MediaStore.Images.Media.DATE_MODIFIED " desc");
if(mCursor!=null){
while (mCursor.moveToNext()) {
// 获取图片的路径
String path = mCursor.getString(mCursor.getColumnIndex(MediaStore.Images.Media.DATA));
int size = mCursor.getInt(mCursor.getColumnIndex(MediaStore.Images.Media.SIZE))/1024;
String displayName = mCursor.getString(mCursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME));
//用于展示相册初始化界面
if(path.contains("/storage/emulated/0/messageBoard/photoImgs")){
mediaBeen.add(new MediaBean(path,size,displayName));
}
//            // 获取该图片的父路径名
//            String dirPath = new File(path).getParentFile().getAbsolutePath();
//            
//            //存储对应关系
//            if (allPhotosTemp.containsKey(dirPath)) {
//              List<MediaBean  data = allPhotosTemp.get(dirPath);
//              data.add(new MediaBean(path,size,displayName));
////              Log.e(TAG,"getAllPhotoInfo " data.size() ",path=" data.get(0).getPath() ",name=" data.get(0).getDisplayName());
//              continue;
//            } else {
//              List<MediaBean  data = new ArrayList< ();
//              data.add(new MediaBean(path,size,displayName));
//              allPhotosTemp.put(dirPath,data);
////              Log.e(TAG,"getAllPhotoInfo else " data.size() ",path=" data.get(0).getPath() ",name=" data.get(0).getDisplayName());
//            }
}
mCursor.close();
}
//更新界面
runOnUiThread(new Runnable() {
@Override
public void run() {
//...
Log.e(TAG,"mediaBeen=" mediaBeen.size());
albumAdapter = new AlbumAdapter(AvaterActivity.this,mediaBeen);
gvAlbum.setAdapter(albumAdapter);
gvAlbum.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?  parent, View view, int position, long id) {
Intent cropIntent = new Intent(mContext, CropImgActivity.class);
cropIntent.putExtra("status",status);
cropIntent.putExtra("takepath",mediaBeen.get(position).getPath());
startActivity(cropIntent);
}
});
}
});
}
}).start();
}

以上这篇android 获取手机中的所有图片或某一目录下的图片方法就是小编分享给大家的全部内容了,希望能给大家一个参考。

0 人点赞