1

i put some images into the res/drawable folder. They are named (s1.png, s2.png, s3png.., 2n.png).

I want to loop (and then process) them. I would do that like:

    for (int i = 1; i < n; i++) {
        HavingFunWithPNGS(R.drawable.s + IntToStr('i'));
    }

Ofcourse thats not how it works. How does it? Thanks in advance.

Edit: My coreproblem is, to convert the filename-strings to the corresponding RessourceID's that android-studio assigns.

3 Answers3

1

Expanding on @mrek's answer, if you are using shrinkResources don't forget to add your ids to a keep file. Otherwise they will be removed.

//file res/raw/keep.xml

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
    tools:keep="@drawable/s*,@drawable/2n"/>

" * " is a wildcard, so this would cover s1, s2, s3 etc.

See http://tools.android.com/tech-docs/new-build-system/resource-shrinking for details

getResources() and getPackageName() are methods of Context. You will need a reference to a context to use these.

Alexander Zhak
  • 9,140
  • 4
  • 46
  • 72
Oguz Babaoglu
  • 299
  • 2
  • 7
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Markus W Mahlberg Apr 14 '15 at 21:31
  • @MarkusWMahlberg edited answer to include relevant information. – Oguz Babaoglu Apr 14 '15 at 21:52
0

If you want to get drawable id, do it this way (passing it as a String imageName):

int id = getResources().getIdentifier(imageName, type, package);

If you want to see the code, please look at this answer. You should pass the image name in String variable, so I think you should rather use it this way:

String imageName = "s" + i;

because it looks better, you don't need to convert anything (java will do it automatically) and it is easily readable.

Community
  • 1
  • 1
mrek
  • 1,655
  • 1
  • 21
  • 34
  • This is good, however i dont have access to getResources() from my java class... but i am allowed to reference it by R.drawable.s1 Why? – Jonas Shinaniganz Apr 14 '15 at 20:15
  • Method [getResources()](http://developer.android.com/reference/android/content/Context.html#getResources()) is in Context object, and if your class does not extend it (like Activity) or you cannot get it (like in Fragment using getActivity()) you have to pass it manually, usually through [constructor](http://stackoverflow.com/a/17918056/3894977). If you are willing to tell me what kind of class do you have, I can help you more. – mrek Apr 16 '15 at 20:11
0

Try to call HavingFunWithPNGS(ResUtil.getInstance().getDrawableIdByName(this, String.valueOf("s"+i))). And below are codes of ResUtil:

public class ResUtil {
private static ResUtil instance;

private ResUtil() {
}

public final static ResUtil getInstance() {
    if (instance == null) {
        instance = new ResUtil();
    }
    return instance;
}

private final int getIdByNames(Context ctx, String resType, String fileName) {
    if (ctx == null || resType == null || fileName == null || "".equals(resType) || "".equals(fileName)) {
        return 0;
    }
    int id = ctx.getResources().getIdentifier(fileName, resType, ctx.getPackageName());
    return id;
}

public final int getAnimIdByName(Context ctx, String fileName) {
    return getIdByNames(ctx, "anim", fileName);
}

public final int getAnimatorIdByName(Context ctx, String fileName) {
    return getIdByNames(ctx, "animator", fileName);
}

public final int getArrayIdByName(Context ctx, String fileName) {
    return getIdByNames(ctx, "array", fileName);
}

public final int getAttrIdByName(Context ctx, String fileName) {
    return getIdByNames(ctx, "attr", fileName);
}

public final int getBoolIdByName(Context ctx, String fileName) {
    return getIdByNames(ctx, "bool", fileName);
}

public final int getColorIdByName(Context ctx, String fileName) {
    return getIdByNames(ctx, "color", fileName);
}

public final int getDimenIdByName(Context ctx, String fileName) {
    return getIdByNames(ctx, "dimen", fileName);
}

public final int getDrawableIdByName(Context ctx, String fileName) {
    return getIdByNames(ctx, "drawable", fileName);
}

public final int getFractionIdByName(Context ctx, String fileName) {
    return getIdByNames(ctx, "fractiont", fileName);
}

public final int getIdByName(Context ctx, String fileName) {
    return getIdByNames(ctx, "id", fileName);
}

public final int getIntegerIdByName(Context ctx, String fileName) {
    return getIdByNames(ctx, "integer", fileName);
}

public final int getInterpolatorIdByName(Context ctx, String fileName) {
    return getIdByNames(ctx, "interpolator", fileName);
}

public final int getLayoutIdByName(Context ctx, String fileName) {
    return getIdByNames(ctx, "layout", fileName);
}

public final int getMenuIdByName(Context ctx, String fileName) {
    return getIdByNames(ctx, "menu", fileName);
}

public final int getMipmapIdByName(Context ctx, String fileName) {
    return getIdByNames(ctx, "mipmap", fileName);
}

public final int getPluralsIdByName(Context ctx, String fileName) {
    return getIdByNames(ctx, "plurals", fileName);
}

public final int getRawIdByName(Context ctx, String fileName) {
    return getIdByNames(ctx, "raw", fileName);
}

public final int getStringIdByName(Context ctx, String fileName) {
    return getIdByNames(ctx, "string", fileName);
}

public final int getStyleIdByName(Context ctx, String fileName) {
    return getIdByNames(ctx, "style", fileName);
}

public final int getStyleableIdByName(Context ctx, String fileName) {
    return getIdByNames(ctx, "styleable", fileName);
}

public final int getTransiitonIdByName(Context ctx, String fileName) {
    return getIdByNames(ctx, "transition", fileName);
}

public final int getXmlIdByName(Context ctx, String fileName) {
    return getIdByNames(ctx, "xml", fileName);
}
}
SilentKnight
  • 13,761
  • 19
  • 49
  • 78