簡體   English   中英

我無法設置ImageBitmap

[英]I cannot setImageBitmap

我從數據庫中獲取圖像(字符串),然后將其轉換為字節數組,但未在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>

嘗試這段代碼....

byte[] imageByte = Base64.decode(imageString,Base64.NO_WRAP);
InputStream inputStream  = new ByteArrayInputStream(imageByte);
Bitmap bmp = BitmapFactory.decodeStream(inputStream);
imageView.setImageBitmap(bmp);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM