Android获取图片Uri/path
16lz
2021-01-23
ANDROID 4.4以上:
display函数
@TargetApi(19)private void handleImageOnKitKat(Intent data){ String imagePath = null; Uri uri = data.getData(); if(DocumentsContract.isDocumentUri(this,uri)){ String docID = DocumentsContract.getDocumentId(uri); if("com.android.providers.media.documents".equals(uri.getAuthority())) { String id = docID.split(":")[1]; 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); } else if ("file".equalsIgnoreCase(uri.getScheme())){ imagePath = uri.getPath(); } displayImage(imagePath); }
4.4以下
private void handleImageBeforeKitKat(Intent data){ Uri uri = data.getData(); String imagePath = getImagePath(uri,null); displayImage(imagePath); }
private String getImagePath(Uri uri, String selection){ String path = null; Cursor cursor = getContentResolver().query(uri,null,selection,null,null); if(cursor != null){ path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)); } cursor.close(); return path;}
display函数
private void displayImage(String imagePath){ if(imagePath != null){ BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 2; Bitmap bitmap = BitmapFactory.decodeFile(imagePath,options); showview.setImageBitmap(bitmap); } else{ Toast.makeText(this,"failed to get image",Toast.LENGTH_SHORT).show(); } }}
更多相关文章
- C语言函数以及函数的使用
- H5 Web网页通过JS(JavaScript)脚本调用Android本地原生方法函数
- android 回调函数二:应用实例
- android NDK JNI设置自己的log输出函数
- Android build/envsetup.sh 脚本分析(lunch函数)
- Android Hook学习之ptrace函数的使用
- Android中回调函数的理解---本人Android纯新手
- 【Android NDK 开发】Ubuntu 函数库交叉编译 ( Android 动态库交
- Android学习札记12:对Parcelable中describeContents()函数的一种