Search code examples
androidbitmapbitmapfactory

How can one get the ID of a jpg saved in res/drawable-hdpi folder?


I have a jpeg. I dragged it into an auto-generated folder called drawable-hdpi under the res folder. All the tutorials just use "R.drawable.myimage" but I get an error "cannot be resolved to a variable" Below is my code:

import android.R;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;

public class MyCanvas extends View{

    Bitmap bmp;

    public MyCanvas(Context context) {
        super(context);
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inJustDecodeBounds = true;
        bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.85849);   
    }

}

Note that the name of my jpg file is 85849


Solution

  • You have two problems. First, you need to use the correct R. Get rid of the line

    import android.R;
    

    or else qualify the R in your decodeResource call with your app's package name (or import the correct R).

    Second, your resource file names cannot start with a number. They have to be legal Java identifiers.