如何从资产中显示图像 - 来自CursorAdapter的图像名称?

时间:2018-01-16 03:48:54

标签: android listview android-imageview android-cursoradapter

拥有_id,hName,lName,detail,images

的数据库

存储在资产中的图像

db(SQLite

中的图像名称

列表布局没有图像工作正常,但图像布局显示名称不是图像(logcat没有错误)

希望将CursorAdpater绑定到“ImageView”

 public void bindView(View view, Context context, Cursor cursor) {
        TextView hName =  view.findViewById(R.id.hName);
        hName.setText(cursor.getString(cursor.getColumnIndex("hName")));

        TextView lName =  view.findViewById(R.id.lName);
        lName.setText(cursor.getString(cursor.getColumnIndex("lName")));

        ImageView image = view.findViewById(R.id.image);
        InputStream is = null;
        try {

            is = context.getAssets().open("birds/" + cursor.getColumnIndex("images"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        Bitmap bitmap = BitmapFactory.decodeStream(is);
        image.setImageBitmap(bitmap);


    }

XML文件:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">


<ImageView
    android:id="@+id/image"
    android:scaleType="fitCenter"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:padding="24dp"
     />

<TextView
    android:id="@+id/hName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/image"
    android:layout_marginLeft="21dp"
    android:layout_marginStart="10dp"
    android:layout_toEndOf="@+id/image"
    android:layout_toRightOf="@+id/image"
    android:text="Name"
    android:textSize="20sp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/lName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/image"
    android:layout_alignLeft="@+id/hName"
    android:layout_alignStart="@+id/hName"
    android:textSize="16sp"
    android:text="About" />

1 个答案:

答案 0 :(得分:0)

检查数据库中使用的图像extension 然后尝试这个

try {
         is = context.getAssets().open("birds/" + cursor.getColumnIndex("images"));
         Drawable d = Drawable.createFromStream(is, null);
         image.setImageDrawable(d);

    } catch (IOException e) {
                    e.printStackTrace();
    }