繁体   English   中英

使用资产文件夹中的图像而不是ListFragment中的可绘制文件夹

[英]use images from assets folder instead of drawable folder in a ListFragment

即时消息很难找出如何以与可绘制文件夹相同的方式使用资产文件夹中的图像。 我已经搜索并发现我可以使用getAssets()函数,但是我不知道如何使用它,我在片段中有服装列表视图。 我不想使用drawable文件夹,因为我的资产包含一些我需要的子文件夹。

在此处输入图片说明

这是我到目前为止所做的,但是图像来自可绘制文件夹,我想用资产中的图像重新填充它们:

public class AfricaFragment extends ListFragment{

private AssetManager assets;

/** An array of items to display in ArrayList */
String android_versions[] = new String[]{
        "flag1",
        "flag2",
        "flag3",
        "flag4"
};
Integer[] imgid={ 
        R.drawable.a,
        R.drawable.b,
        R.drawable.c,
        R.drawable.d
};

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

    assets = getActivity().getBaseContext().getAssets();
    InputStream stream;


    CustomListAdapter adapter=new CustomListAdapter(getActivity(), android_versions, imgid);
    this.setListAdapter(adapter);

    return super.onCreateView(inflater, container, savedInstanceState);

}

@Override
public void onStart() {
    super.onStart();
    /** Setting the multiselect choice mode for the listview */
    getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

}
}

我很感谢您的帮助。

要从资产获取图像:

try {
        // Use image name with extension
        InputStream ims = getActivity().getAssets().open("flag1.jpg");
        Drawable d = Drawable.createFromStream(ims, null);
        iv.setImageDrawable(d);
        ims .close();
    }
    catch(IOException ex) {
        return;
    }

试试这个代码

    public Bitmap getBitmapFromAssets(String fileName) {
        AssetManager assetManager = getAssets();

        InputStream is = null;
        try{
            is = assetManager.open(fileName);
        }catch(IOException e){
            e.printStackTrace();
        }
        Bitmap bitmap = BitmapFactory.decodeStream(is);

        return bitmap;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM