我从我的数据库中获取一个图像(字符串),然后我将这个字符串转换为字节数组,但它没有显示在ImageView
以下是我的活动代码。
protected Void doInBackground(Void... params) {
try {
imageString = WallPaper.getWallPapers(); // return the string for image
runOnUiThread(new Runnable() {
@Override
public void run()
{
byte [] imageByte = Base64.decode(imageString, Base64.DEFAULT);
Bitmap bmp = BitmapFactory.decodeByteArray(imageByte, 0, imageByte.length);
imageView.setImageBitmap(bmp);
}
});
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
布局:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/app_bar_main"
tools:context="com.example.burak.apps.MainActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView2"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
答案 0 :(得分:0)
尝试使用这段代码......
byte[] imageByte = Base64.decode(imageString,Base64.NO_WRAP);
InputStream inputStream = new ByteArrayInputStream(imageByte);
Bitmap bmp = BitmapFactory.decodeStream(inputStream);
imageView.setImageBitmap(bmp);