替代的阿卡什Kurian說何塞答案
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
我始終使用
fun getBitmap(file: Uri, cr: ContentResolver): Bitmap?{
var bitmap: Bitmap ?= null
try {
val inputStream = cr.openInputStream(file)
bitmap = BitmapFactory.decodeStream(inputStream)
// close stream
try {
inputStream.close()
} catch (e: IOException) {
e.printStackTrace()
}
}catch (e: FileNotFoundException){}
return bitmap
}
它同時適用從畫廊的照片和照相機的照片。
它
更大的問題:使用這種方法Picasso unable to display image from Gallery
開畫廊:
private void openGallery(){
if (Build.VERSION.SDK_INT <19){
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, getResources().getString(R.string.select_picture)),GALLERY_INTENT_CALLED);
} else {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(intent, GALLERY_KITKAT_INTENT_CALLED);
}
}
那你就能夠讀取轉換烏里到位圖使用afromentioned ContentResolver.openInputStream
或設置圖片ImageView.setImageUri(Uri)
你能得到圖像路徑嗎? –