Android开发模板代码(一)——简单打开图库选择照片

首先,先贴上样本代码

//检查权限 public void checkPermission() { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1); //发现没有权限,调用requestPermissions方法向用户申请权限,requestPermissions接收三个参数,第一个是context,第二个是一个String数组,我们把要申请的权限 //名放在数组中即可,第三个是请求码,只要是唯一值就行 } else { openAlbum();//有权限就打开相册 } } public void openAlbum() { //通过intent打开相册,使用startactivityForResult方法启动actvity,会返回到onActivityResult方法,所以我们还得复写onActivityResult方法 Intent intent = new Intent("android.intent.action.GET_CONTENT"); intent.setType("image/*"); startActivityForResult(intent, CHOOSE_PHOTO); } //弹出窗口向用户申请权限 @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults);//弹出授权的窗口,这条语句也可以删除,没有影响 //获得了用户的授权结果,保存在grantResults中,判断grantResult中的结果来决定接下来的操作 switch (requestCode) { case 1: if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { openAlbum(); } else { Toast.makeText(this, "授权失败,无法操作", Toast.LENGTH_SHORT).show(); } break; default: break; } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case CHOOSE_PHOTO: if (resultCode == RESULT_OK) { if (Build.VERSION.SDK_INT >= 19) { handleImageOnkitKat(data);//高于4.4版本使用此方法处理图片 } else { handleImageBeforeKitKat(data);//低于4.4版本使用此方法处理图片 } } break; default: break; } } @TargetApi(19) private void handleImageOnkitKat(Intent data) { String imagePath = null; Uri uri = data.getData(); if (DocumentsContract.isDocumentUri(this, uri)) { //如果是document类型的uri,则通过document id处理 String docId = DocumentsContract.getDocumentId(uri); if ("com.android.providers.media.documents".equals(uri.getAuthority())) { String id = docId.split(":")[1];//解析出数字格式的id String selection = MediaStore.Images.Media._ID + "=" + id; imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection); } else if ("com.android,providers.downloads.documents".equals(uri.getAuthority())) { Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(docId)); imagePath = getImagePath(contentUri, null); } } else if ("content".equalsIgnoreCase(uri.getScheme())) { imagePath = getImagePath(uri, null); } displayImage(imagePath); } private void handleImageBeforeKitKat(Intent data) { Uri uri = data.getData(); String imagePath = getImagePath(uri, null); displayImage(imagePath); } //获得图片路径 public String getImagePath(Uri uri, String selection) { String path = null; Cursor cursor = getContentResolver().query(uri, null, selection, null, null); //内容提供器 if (cursor != null) { if (cursor.moveToFirst()) { path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)); //获取路径 } } cursor.close(); return path; } //展示图片 private void displayImage(String imagePath) { if (imagePath != null) { Bitmap bitImage = BitmapFactory.decodeFile(imagePath);//格式化图片 mImage.setImageBitmap(bitImage);//为imageView设置图片 } else { Toast.makeText(MainActivity.this, "获取图片失败", Toast.LENGTH_SHORT).show(); } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zywspw.html