本文主要是介绍Android踩坑日记:监听软键盘多次调用和刷新系统相册和获取所有相片,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
EditText设置监听软键盘删除键(或enter)
- 一般使用方法
edittext.setOnKeyListener(new View.OnKeyListener(){@Overridepublic boolean onKey(View v, int keyCode, KeyEvent event) {if (keyCode == KeyEvent.KEYCODE_DEL ){//处理操作}}
});
问题:
当点击软键盘的删除键时,处理操作会只执行两次或多次爬坑姿势:
edittext.setOnKeyListener(new View.OnKeyListener(){@Overridepublic boolean onKey(View v, int keyCode, KeyEvent event) {/*必须是这两个条件,否则会执行多次*/if (keyCode == KeyEvent.KEYCODE_DEL && event.getAction()==KeyEvent.ACTION_DOWN){//处理操作}}
});
刷新系统相册
当我们在系统的相册中增加或者删除图片,需要再次获取系统相册的所有图片时,需要通知系统刷新一下系统相册
/*通知系统刷新相册*/public static void noticeSystemRefreshAlbum(Context context,String path){Intent mediaScanIntent=new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);Uri uri=null;if (path!=null){uri= Uri.fromFile(new File(path));}mediaScanIntent.setData(uri);mediaScanIntent = PermissionUtil.getUriAuthority(mediaScanIntent);context.sendBroadcast(mediaScanIntent);}public static Intent getUriAuthority(Intent intent) {//对于android M及更高版本,intent需要提供URI相关权限以操作文件//文件uri需要通过FileProvider获取if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);}return intent;}
获取系统所有图片和按相册名分类
/*本地相片文件描述类*/
public class LocalPhotoBean implements Serializable{/** 系统数据库中的id */public long mediaId;/*文件名*/public String fileName;/*文件路径*/public String path;/*缩略图路径*/public String thumbNailPath;/*文件大小*/public long fileSize;/*所属文件夹名称*/public String albumName;/*文件夹识别id*/public String albumId;/*缓存用于显示的路径*/private String showPicPath;
}
/*相册文件夹描述类*/
public class PhotoAlbumBean implements Serializable{/*文件夹识别Id*/public String albumId;/*文件夹名称*/public String albumName;/*文件夹封面路径*/public String albumCoverPath;/*文件夹封面缩略图路径*/public String albumCoverThumbNailPath;/*文件夹包含的图片*/public ArrayList<LocalPhotoBean> imageList;/*缓存的用于显示的路径*/private String showPicPath;}/*查询返回结果集*/public static class LocalPhotoDataList{//所有图片列表public ArrayList<LocalPhotoBean> photoList;//相册文件夹列表public ArrayList<PhotoAlbumBean> albumList;public LocalPhotoDataList(ArrayList<LocalPhotoBean> photoList, ArrayList<PhotoAlbumBean> albumList) {this.photoList = photoList;this.albumList = albumList;}}/*** 通过ContentResolver ,查询本地图片信息* @param resolver* @return*/public static LocalPhotoDataList getLocalPhotos(ContentResolver resolver, int minImageSize){ArrayList<LocalPhotoBean> photoList=new ArrayList<>();HashMap<String ,PhotoAlbumBean> albumMap=new HashMap<>();ArrayList<PhotoAlbumBean> albumList=new ArrayList<>();Cursor cursor=null;try {//需要查询的数据'String[] queryColumns={MediaStore.Images.ImageColumns.DISPLAY_NAME,//图片名MediaStore.Images.ImageColumns.DATA,//图片路径MediaStore.Images.ImageColumns._ID,//图片数据库MediaStore.Images.ImageColumns.BUCKET_ID,//图片所在文件夹IdMediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME,//图片所在文件夹文件名MediaStore.Images.ImageColumns.SIZE,//图片大小MediaStore.Images.ImageColumns.MIME_TYPE,//图片格式MediaStore.Images.ImageColumns.MINI_THUMB_MAGIC,//缩略图IdMediaStore.Images.ImageColumns.DATE_MODIFIED//修改时间};//查询的条件String selection=MediaStore.Images.ImageColumns.SIZE +" >= ? AND "+MediaStore.Images.ImageColumns.MIME_TYPE + " != ? ";//排序规则 修改时间降序排列String orderBy=MediaStore.Images.ImageColumns.DATE_MODIFIED+" DESC";String[] args=new String[]{String.valueOf(minImageSize),"image/gif"};//查询资源数据cursor=resolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,queryColumns,selection,args,orderBy);if (cursor!=null&&cursor.moveToFirst()){int IndexName=cursor.getColumnIndex(MediaStore.Images.ImageColumns.DISPLAY_NAME);int IndexPath=cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);int IndexId=cursor.getColumnIndex(MediaStore.Images.ImageColumns._ID);int IndexBucket=cursor.getColumnIndex(MediaStore.Images.ImageColumns.BUCKET_ID);int IndexBucketName=cursor.getColumnIndex(MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME);int IndexSize=cursor.getColumnIndex(MediaStore.Images.ImageColumns.SIZE);int IndexThumbId=cursor.getColumnIndex(MediaStore.Images.ImageColumns.MINI_THUMB_MAGIC);do{LocalPhotoBean photoItem=new LocalPhotoBean().setMediaId(cursor.getLong(IndexId)).setFileName(cursor.getString(IndexName)).setPath(cursor.getString(IndexPath)).setAlbumName(cursor.getString(IndexBucketName)).setAlbumId(cursor.getString(IndexBucket)).setFileSize(cursor.getLong(IndexSize))/*暂时缩略图*/;photoList.add(photoItem);//刷新文件夹集合数据PhotoAlbumBean photoAlbum=albumMap.get(photoItem.albumId);if (photoAlbum==null){photoAlbum=PhotoAlbumBean.getInstance(photoItem.albumName,photoItem.albumId);albumMap.put(photoItem.albumId,photoAlbum);}photoAlbum.addItem(photoItem);}while (cursor.moveToNext());}/*相册文件夹列表*/albumList.addAll(albumMap.values());/*排序*/Collections.sort(albumList);return new LocalPhotoDataList(photoList, albumList);}catch (SecurityException se) {} catch (Exception e) {e.printStackTrace();LogUtils.d(ApiConstants.MEDIA_LOG_TAG, "Error getLocalPhotos");} finally {if (cursor != null) {cursor.close();}}return null;}
这篇关于Android踩坑日记:监听软键盘多次调用和刷新系统相册和获取所有相片的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!