gpt4 book ai didi

android - 另一个类中的错误 getResource()

转载 作者:行者123 更新时间:2023-11-30 00:12:37 26 4
gpt4 key购买 nike

这段代码在mainActivity类中

错误:

 cannot find symbol method getResources()

代码:

public void printPhoto(int img) {

try {
Bitmap bmp = BitmapFactory.decodeResource(getResources(),
img);
if(bmp!=null){
byte[] boleh = Utils.decodeBitmap(bmp);
mmOutputStream.write(PrinterCommands.ESC_ALIGN_CENTER);
printText(boleh);
}else{
Log.e("Print Photo error", "the file isn't exists");
}

} catch (Exception e) {
e.printStackTrace();
Log.e("PrintTools", "the file isn't exists");
}

}

All of my function and method in another class. In my MainActivity just for button and listener. how to solve this. I am beginner in android studio. thanks

最佳答案

你应该通过 Context .

代码

public void printPhoto(Context ctx,int img) {

try {
Bitmap bmp = BitmapFactory.decodeResource(ctx.getResources(),
img);

调用

printPhoto(YourActivityName.this, R.drawable.your_image); // For Activity
printPhoto(getActivity(), R.drawable.your_image); // For Fragment

关于android - 另一个类中的错误 getResource(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47920093/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com