[英]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.