Android调⽤相册返回路径以及返回Uri的总结今天在做调取相册返回的时候, 出现⼀种新型的类型, 也许是我以前没碰到过这种类型吧.如下
content://com.android.providers.downloads.documents/document/raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2Ftest.jpg
这个转换⽤以前使⽤的⽅法转换不成功,emmm, 很久没使⽤过Android了吧,有点头蒙了.下⾯是以前的使⽤⽅法
//获取权限
private void getPermission() {
//如果没有权限,Android6.0之后,必须动态申请权限(记住这个套路)
if (ContextCompat.checkSelfPermission(MainActivity2.this,
Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
//如果⽤户已经拒绝过⼀次权限申请,该⽅法返回true
if (ActivityCompat.shouldShowRequestPermissionRationale(
MainActivity2.this, Manifest.permission.READ_EXTERNAL_STORAGE)) {
//提⽰⽤户这⼀权限的重要性
Toast.makeText(MainActivity2.this, "读取SD卡功能是本应⽤的核⼼"
+ "功能,如果不授予权限,程序是⽆法正常⼯作!",
Toast.LENGTH_SHORT).show();
}
//请求权限
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
} else { //权限已被授予,打开相册
openAlbum();
}
}
@Override
//弹出⼀个权限申请的对话框,并且⽤户做出选择后,该⽅法⾃动回调
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
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;
}
}
private void openAlbum() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if (requestCode == 1) {
// 判断⼿机版本
if (Build.VERSION.SDK_INT >= 19) {
/
/ 4.4及以上系统使⽤这个⽅法处理图⽚
handleImageOnKitKat(data);
} else {
// 4.4以下系统使⽤这个⽅法处理图⽚
handleImageBeforeKitKat(data);
}
}
}
@TargetApi(19)
private void handleImageOnKitKat(Intent data) {
String imagePath = null;
Uri uri = Data();
Log.d("TAG", "handleImageOnKitKat: uri is " + uri);
Log.d("TAG", "handleImageOnKitKat : " + Authority());
Log.d("TAG", "handleImageOnKitKat urlPath: " + Scheme());
if (DocumentsContract.isDocumentUri(this, uri)) {
// 如果是document类型的Uri,则通过document id处理
String docId = DocumentId(uri);
Log.d("TAG", "handleImageOnKitKat: docId" + docId);
if ("com.dia.documents".Authority())) {
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".Authority())) {
Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.parseLong(docId));
imagePath = getImagePath(contentUri, null);
}
} else if ("content".Scheme())) {
// 如果是content类型的Uri,则使⽤普通⽅式处理
imagePath = getImagePath(uri, null);
} else if ("file".Scheme())) {
// 如果是file类型的Uri,直接获取图⽚路径即可
imagePath = Path();
}
displayImage(imagePath); // 根据图⽚路径显⽰图⽚
}
private void handleImageBeforeKitKat(Intent data) {
Uri uri = Data();
String imagePath = getImagePath(uri, null);
displayImage(imagePath);
}
private String getImagePath(Uri uri, String selection) {
String path = null;
// 通过Uri和selection来获取真实的图⽚路径
Cursor cursor = getContentResolver().query(uri, null, selection, null, null);
if (cursor != null) {
if (veToFirst()) {
path = ColumnIndex(MediaStore.Images.Media.DATA));
}
cursor.close();
}
Log.e("TAG", "getImagePath: " + path);
return path;
}
private void displayImage(String imagePath) {
this.imagePath = imagePath;
if (imagePath != null) {
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
ivAvatarEdit.setImageBitmap(bitmap);
document有安卓版吗} else {
Toast.makeText(this, "failed to get image", Toast.LENGTH_SHORT).show();
}
}
之前的路径为:
content://com.dia.documents/document/image%3A128991
然后现在的路径为:
content://com.android.providers.downloads.documents/document/raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2Ftest.jpg
这个我测试了下, 会报java.lang.NumberFormatException异常, 通过查, 就是这⾏代码错误, 如下
Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.parseLong(docId));
docId不能转换为长整型
通过查看log⽇志:docId, 发现, 其实他已经是⼀个⽂件路径了, 如:draw:/storage/emulated/0/Download/test.jpg, 可以直接通过截取给替换掉就可以当成路径使⽤了, 替代代码如下
在我上⽅的handleImageOnKitKat函数⾥else if中修改
      else if ("com.android.providers.downloads.documents".Authority())) {
// 下载⽬录
if (docId.startsWith("raw:")) {
imagePath = placeFirst("raw:", "");
} else {
Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.parseLong(docId));
imagePath = getImagePath(contentUri, null);
}
}
 这样问题就解决了, 如果有不会的可以留⾔,⼀起探讨交流.

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。