Android:我可以访问我自己的类中的资源吗?
在我的项目中,我有一个类,在这个类中我想访问图片的属性。但我不想通过这堂课来展示它。在这个课程中,我只想知道图像的宽度和高度,并执行一些数学函数来返回一些内容。
我的问题是我无法访问资源。例如,当我写它时
image = BitmapFactory.decodeResource(this.getResources(), R.drawable.tasnim);
,它说 Stego 类型的 getResources() 方法未定义(stego 是我的类名)。
请告诉我该怎么办?
public class Stego()
{
Public Stego(){
image = BitmapFactory.decodeResource(this.getResources(), R.drawable.tasnim);
imWidth = image.getWidth();
imHeight = image.getHeight();
}
}
I my project I have a class that in this class I want to have access to properties of a picture. But I don't want to show it through this class. In this class I just want to know width and height of image and do some mathematical functions to return something.
My problem is I can't have access to resources. for example, when I write
image = BitmapFactory.decodeResource(this.getResources(), R.drawable.tasnim);
It says the method getResources() in undefined for the type Stego (stego is my class name).
Please tell me what should I do?
public class Stego()
{
Public Stego(){
image = BitmapFactory.decodeResource(this.getResources(), R.drawable.tasnim);
imWidth = image.getWidth();
imHeight = image.getHeight();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
getResources()
函数属于Context
类。因此,除非您的Stego
类直接(或间接)扩展Context
,否则没有可用的getResources()
函数。因此,您需要做的就是传递对应用程序上下文的引用(例如,在
Stego
类或类似类的构造函数中)。或者,如果您只需要一个资源 ID,那么传递该 ID 可能会更容易。例如,这可能如下所示:
然后,您可以从您的活动中初始化
Stego
类的实例,如下所示:The
getResources()
function belongs to theContext
class. So unless yourStego
class directly (or indirectly) extendsContext
, there is nogetResources()
function available.So what you need to do is pass along a reference to your application's context (e.g. in the constructor of your
Stego
class or similar). Or if all you need is a single resource ID it might be easier to just pass along that ID.This could for instance look like this:
From your activity you can then initialize an instance of your
Stego
class like this: