Android将位图设置为Imageview
问题描述:
嗨,我有一个Base64格式的字符串。我想将它转换为位图,然后将其显示到ImageView。这是代码:Android将位图设置为Imageview
ImageView user_image;
Person person_object;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_profile_screen);
// ImageViews
user_image = (ImageView) findViewById(R.id.userImageProfile);
Bundle data = getIntent().getExtras();
person_object = data.getParcelable("person_object");
// getPhoto() function returns a Base64 String
byte[] decodedString = Base64.decode(person_object.getPhoto(), Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
user_image.setImageBitmap(decodedByte);
}
此代码获得Base64字符串成功,我没有得到任何错误。但它不显示图像。 有什么问题? 感谢
答
请试试这个:
byte[] decodedString = Base64.decode(person_object.getPhoto(),Base64.NO_WRAP);
InputStream inputStream = new ByteArrayInputStream(decodedString);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
user_image.setImageBitmap(bitmap);
答
有一个名为毕加索库,能够有效地从URL加载图像。它也可以从文件加载图像。
实例:
-
加载URL到ImageView的,而不会产生一个位图:通过产生位图
Picasso.with(context) // Context .load("http://abc.imgur.com/gxsg.png") // URL or file .into(imageView); // An ImageView object to show the loaded image
-
负载网址的ImageView:
Picasso.with(this) .load(artistImageUrl) .into(new Target() { @Override public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) { /* Save the bitmap or do something with it here */ // Set it in the ImageView theView.setImageBitmap(bitmap) } @Override public void onBitmapFailed(Drawable errorDrawable) { } @Override public void onPrepareLoad(Drawable placeHolderDrawable) { } });
有更多选择在毕加索可用的离子。 Here is the documentation。
请尝试添加此行:user_image.setScaleType(ScaleType.FIT_XY); – KEYSAN 2013-03-10 16:02:56
它是否适用于资源图片?例如,如果您编写'iuser_image.setImageResource(android.R.drawable.ic_delete)',它会显示任何内容吗? – vorrtex 2013-03-10 16:42:46