在res文件夹中有自动生成的drawable-hdpi文件夹, 其中有一个jpeg.文件。我先要获取它的ID时参考资料上写"R.drawable.myimage" ,我用完后报错误: "cannot be resolved to a variable"
代码:
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);
}
}
jpg文件的名臣是85849
drawable-hdpi下可以创建目录,当编译器不能的在R.java中的生成ID,
如果图片很多可以考虑放在assets下
你的包引错了,不应该引用android.R
不要用数字作为图片资源的名字,用字母试试。
int resID = getResources().getIdentifier("图片名字", "drawable",“包名”);