在我的应用中,用户点击按钮打开选择器以选择图片。在我的活动结果中,代码是
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE_REQUEST_CODE) {
Uri selectedImageUri = data.getData();
if(selectedImageUri!=null){
imageviewTest.setImageURI(selectedImageUri)
}else{
}
}
}
}
不显示图像。我想知道为什么?我注意到这个论坛上的一些人将这个值传递给了contentresolver,为什么会这样?路径不应该足够吗?
由于
答案 0 :(得分:0)
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE_REQUEST_CODE) {
BitmapFactory.Options bfOptions =new BitmapFactory.Options();
Uri selectedImage = data.getData();
if(selectedImage!=null){
String galleryImatePath = getRealPathFromURI(selectedImage);
try{
bfOptions.inDither=false; //Disable Dithering mode
bfOptions.inPurgeable=true; //Tell to gc that whether it needs free memory, the Bitmap can be cleared
bfOptions.inInputShareable=true; //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
bfOptions.inSampleSize=5;
bfOptions.inTempStorage=new byte[32 * 1024];
InputStream stream = getContentResolver().openInputStream(selectedImage);
final Bitmap myImage = BitmapFactory.decodeStream(stream, null , bfOptions);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
myImage.compress(Bitmap.CompressFormat.JPEG, 100, bos);
bitmapdata = bos.toByteArray();
imageviewTest.setImageBitmap(myImage)
}else{
}
}
}
}
public String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}