简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM