如何从资产中创建一个id图像数组有很多文件夹

问题描述:

我在资产中有很多文件夹和图像,例如:“folder1/img1.jpg,img2.jpg ... img15.jpg; folder2/img1.jpg, img2.jpg ... img20.jpg; ......“。我有一个名为AssetManager中资产的所有文件夹的ListView,当OnClickItem我会加载单击项目(选定的文件夹)中的所有图像。因为我在GitHub使用代码“卷曲页”,我需要ID的阵列加载每个文件夹中的所有图像不使用简单的阵列,用于绘制,例如:如何从资产中创建一个id图像数组有很多文件夹

private int[] mIdsSelectedFolder = { R.drawable.img1, R.drawable.img2,... R.drawable.img15};

然后我加载图像:

Drawable d = getResources().getDrawable(mIdsSelectFolder[index]);

但我不知道如何给每个文件夹中的计数numble图像,然后让所有ID形象,从每个文件夹创建一个数组,通过复制和粘贴图像的名称相同的例子不输入所有资源之上,因为每个文件夹都有不同的图像数量!

+1

标记为'低quality' – Nezam 2013-04-29 13:39:18

+0

+1问得好。从代码片段和描述中可以清楚你想要完成什么。 – 2013-04-29 13:59:12

+0

现在有些问题:为什么你需要一个资源ID数组?你可以给你正在使用的“Curl Page”项目的链接吗? – 2013-04-29 14:02:07

你必须创建服装列表视图。并覆盖getView方法

public View getView(final int position, View convertView, 
      ViewGroup parent) { 

     LayoutInflater inflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
     View view = inflater.inflate(R.layout.userprofilepic, null, true); 

     ImageView img= (ImageView) view.findViewById(R.id.img); 

     img.setImageDrawable(getResources().getDrawable(mIdsSelectFolder[position]);); 



     return view; 
    } 
+0

有点上下文,请 – Blackbelt 2013-04-29 13:41:03

+0

有点解释不会伤害:) – 2013-04-29 13:45:41

您可以尝试使用getIdentifier通过名称获取id。

下面你将看到的示例代码:

int id = getResources().getIdentifier("img" + index , "id", getPackageName()); 
if (id!= null) 
{ 
    Drawable d = getResources().getDrawable(id); 
} 

下面的代码将列出文件驻留在“目录”。

try { 
    AssetManager am = this.getAssets(); 
    String str[] = getAssets().list("directory_name"); 
    for (int i = 0; i < str.length; i++) { 
     Log.d("TAG", str[i]); 
     if (str[i].endsWith(".jpg")) { 
      Drawable drw = Drawable.createFromStream(am.open(str[i]),null); 
     } 
    } 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

你只需要创建特定目录的阵列,并添加内部的文件名。您也可以为此创建自定义类。在我的示例中,如果文件名以JPG结尾,则将从中抽取。

编辑: Drawable.createFomStream()来Drawable.createFromStream()