【问题标题】:How to read many file from raw resource in loop?如何在循环中从原始资源中读取多个文件?
【发布时间】:2012-09-10 10:49:48
【问题描述】:

我有很多文本文件,想把它们放在一个 for 循环中。

我从上次活动中获得了一个具有资源名称的 Extra 并有一个资源名称为的数组 我在原始资源中的文本文件来自 {d0,d1,d2,d3, ...,d79} 我想检查名称和数组名称并将查找名称放入资源中! 我在 (res=R.raw.(d[i])) 上有错误 我的代码:

    int res = 0;
    for (int i = 0; i <=79; i = i + 1) {
        if (s.equals(d[i])){
            res=R.raw.(d[i])
        }
    } 
    inputstream = getResources().openRawResource(res);

【问题讨论】:

标签: java android for-loop resources


【解决方案1】:

我需要在一个地方阅读 6 种不同的资源。

    try
     {
        for (int i = 0; i < 6; i++ )
         {
              String fname = "p" + i;
              int id = context.getResources().getIdentifier(fname, "drawable", "com.example.yourproject");
              if (id == 0)
              {
                  Log.e(TAG, "Lookup id for resource '"+fname+"' failed");
                  // graceful error handling code here
              }
             scoresBm[i] =  (Bitmap) BitmapFactory.decodeResource(context.getResources(), id);
         }
    }
    catch(Exception E)
    {
    }

【讨论】:

    【解决方案2】:

    您可以使用getIdentifier (String name, String defType, String defPackage) 动态获取资源ID,

    ArrayList<Integer> id = new ArrayList<Integer>();
    for (int i = 0; i <= 79; i++) {
      id.add(getResources().getIdentifier("d"+i, "raw", getPackageName()));
    }
    

    现在您将拥有 id ArrayList 中的所有资源 id

    【讨论】:

    • 我放了这个,但我强制关闭??? int id[] = 新的 int[79]; for (int i = 0; i
    猜你喜欢
    • 2011-05-04
    • 1970-01-01
    • 2018-01-25
    • 2011-02-08
    • 2013-10-09
    • 2016-06-17
    • 1970-01-01
    • 2012-06-11
    相关资源
    最近更新 更多