赞
踩
开发经常遇到获取图片问题
/** * 读取手机中所有图片信息 */ private void getAllPhotoInfo() { new Thread(new Runnable() { @Override public void run() { 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}; 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)); //用于展示相册初始化界面 // 获取该图片的父路径名 String dirPath = new File(path).getParentFile().getAbsolutePath(); //存储对应关系 PhotoDirectory photo = new PhotoDirectory(); photo.setCoverPath(path); directories.add(photo); } runOnUiThread(new Runnable() { @Override public void run() { adapter.notifyDataSetChanged(); } }); mCursor.close(); } } }).start(); } /** * 获取手机中所有视频的信息 */ private void getAllVideoInfos(){ new Thread(new Runnable() { @Override public void run() { BitmapFactory.Options options = new BitmapFactory.Options(); options.inDither = false; options.inPreferredConfig = Bitmap.Config.ALPHA_8; ContentResolver contentResolver = getContentResolver(); Cursor mCursor = contentResolver.query( MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, null, null, null); List<PhotoDirectory> list = new ArrayList<>(); if(mCursor!=null){ while (mCursor.moveToNext()) { // 获取视频的路径 int videoId = mCursor.getInt(mCursor.getColumnIndex(MediaStore.Video.Media._ID)); String path = mCursor.getString(mCursor.getColumnIndex(MediaStore.Video.Media.DATA)); /* int duration = mCursor.getInt(mCursor.getColumnIndex(MediaStore.Video.Media.DURATION)); long size = mCursor.getLong(mCursor.getColumnIndex(MediaStore.Video.Media.SIZE))/1024; //单位kb if(size<0){ //某些设备获取size<0,直接计算 Log.e("dml","this video size < 0 " + path); size = new File(path).length()/1024; } String displayName = mCursor.getString(mCursor.getColumnIndex(MediaStore.Video.Media.DISPLAY_NAME)); long modifyTime = mCursor.getLong(mCursor.getColumnIndex(MediaStore.Video.Media.DATE_MODIFIED));//暂未用到 //提前生成缩略图,再获取:http://stackoverflow.com/questions/27903264/how-to-get-the-video-thumbnail-path-and-not-the-bitmap String[] projection = { MediaStore.Video.Thumbnails._ID, MediaStore.Video.Thumbnails.DATA}; Cursor cursor = getContentResolver().query(MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI , projection , MediaStore.Video.Thumbnails.VIDEO_ID + "=?" , new String[]{videoId+""} , null); String thumbPath = ""; while (cursor.moveToNext()){ thumbPath = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Thumbnails.DATA)); } cursor.close(); // 获取该视频的父路径名 String dirPath = new File(path).getParentFile().getAbsolutePath();*/ //存储对应关系 PhotoDirectory photo = new PhotoDirectory(); photo.setCoverPath(path); list.add(photo); } runOnUiThread(new Runnable() { @Override public void run() { for (int i = list.size()-1; i >-1 ; i--) { LogUtil.v(i+"================"); directories.add(list.get(i)); } adapter.notifyDataSetChanged(); } }); mCursor.close(); } } }).start(); }
1.适配android6.0+系统
2.解决部分机型闪退问题
3.解决图片过大oom闪退问题
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。