亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將資產文件夾中的圖像設置為列表視圖?

如何將資產文件夾中的圖像設置為列表視圖?

瀟瀟雨雨 2021-08-06 10:38:11
我有一個名為images1 的資產文件夾,其中有 114 張圖像。我需要用 2 個 textViews 和 1 個 imageView 在 listView 中設置它們。我對 textViews 沒有問題,但我不知道如何將圖像從資產設置為 listView。 我試過了:int ids[] = new int[114];for (int i = 0; i <ids.length; i++) {//<-------- taking ids of all pictures from images1 in assets-folder        try {            String[] images =getAssets().list("images1");            ArrayList<String> listImages = new ArrayList<String>(Arrays.asList(images));            int imgId = getResourceId(this, listImages.get(i),"images1", getPackageName());            ids[i] = imgId;        }        catch(IOException ex) {}    }ArrayList<Map<String, Object>> data = new ArrayList<Map<String, Object>>(            questionTexts.length);//<--------filling listView's textViews and imageView    Map<String, Object> m;    for (int i = 0; i < questionTexts.length; i++) {        m = new HashMap<String, Object>();        m.put(ATTRIBUTE_QUESTION_TEXT, questionTexts[i]);//<-------- textView        m.put(ATTRIBUTE_ANSWER_TEXT, answerTexts[i]);//<-------- textView        m.put(ATTRIBUTE_NAME_IMAGE, ids[i]);//<-------- imageView        data.add(m);        String[] from = { ATTRIBUTE_QUESTION_TEXT, ATTRIBUTE_ANSWER_TEXT,                ATTRIBUTE_NAME_IMAGE };        int[] to = { R.id.listView_item_title, R.id.listView_item_short_description, R.id.listView_image };        SimpleAdapter sAdapter = new SimpleAdapter(this, data, R.layout.item,                from, to);        lvSimple = (ListView) findViewById(R.id.lvSimple);        lvSimple.setAdapter(sAdapter);    }但最后我有白色的田野而不是我的照片。 我知道當你的圖片在 R.drawable 中時如何解決這個問題,但是當它們在資產子文件夾中時如何解決?
查看完整描述

3 回答

?
Cats萌萌

TA貢獻1805條經驗 獲得超9個贊

你可以用這個。


try 

{

    // get input stream

    InputStream ims = getAssets().open("avatar.jpg");

    // load image as Drawable

    Drawable d = Drawable.createFromStream(ims, null);

    // set image to ImageView

    mImage.setImageDrawable(d);

  ims .close();

}

catch(IOException ex) 

{

    return;

}


查看完整回答
反對 回復 2021-08-06
?
蠱毒傳說

TA貢獻1895條經驗 獲得超3個贊

Simple Adapter 只能使用 drawable-folder 或 Uri 中的 ID。但是您可以使用 ViewBinder 使用 setImageBitmap 方法設置圖像。


查看完整回答
反對 回復 2021-08-06
?
智慧大石

TA貢獻1946條經驗 獲得超3個贊

你確定這個圖片應該在assets/文件夾內嗎?為什么不進去res/drawables/?


當然,您可以從資產加載它;)


要獲取資產文件夾中所有文件的列表,請使用以下代碼:


list = getAssets().list("images1")

要從資產加載圖像,您可以使用以下代碼:


fun setImageFromAsset(ImageView view, String filename) { 

    try {

        InputStream is = getAssets().open(filename);

        Drawable drawable = Drawable.createFromStream(is, null);

        view.setImageDrawable(drawable);

    }

    catch(IOException ex) {

        return;

    }

}


查看完整回答
反對 回復 2021-08-06
  • 3 回答
  • 0 關注
  • 166 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號