简体   繁体   English

如何从android的默认资源中获取位图?

[英]How to get Bitmap from android's default resource?

Hi I am doing this using this way嗨,我正在使用这种方式执行此操作

Bitmap bm = BitmapFactory.decodeResource(getResources(), android.R.drawable.list_selector_background);

int c = bm.getPixel(bm.getWidth()/2, bm.getHeight()/2);

but bm is null as gives NullPointerException at second line.但 bm 为null因为在第二行给出NullPointerException

Error:错误:

02-12 18:29:57.568: E/AndroidRuntime(5086):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
02-12 18:29:57.568: E/AndroidRuntime(5086):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
02-12 18:29:57.568: E/AndroidRuntime(5086):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-12 18:29:57.568: E/AndroidRuntime(5086):     at android.os.Looper.loop(Looper.java:137)
02-12 18:29:57.568: E/AndroidRuntime(5086):     at android.app.ActivityThread.main(ActivityThread.java:4424)
02-12 18:29:57.568: E/AndroidRuntime(5086):     at java.lang.reflect.Method.invokeNative(Native Method)
02-12 18:29:57.568: E/AndroidRuntime(5086):     at java.lang.reflect.Method.invoke(Method.java:511)
02-12 18:29:57.568: E/AndroidRuntime(5086):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-12 18:29:57.568: E/AndroidRuntime(5086):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-12 18:29:57.568: E/AndroidRuntime(5086):     at dalvik.system.NativeStart.main(Native Method)
02-12 18:29:57.568: E/AndroidRuntime(5086): Caused by: java.lang.NullPointerException
02-12 18:29:57.568: E/AndroidRuntime(5086):     at com.example.showhide.MainActivity.onCreate(MainActivity.java:38)
02-12 18:29:57.568: E/AndroidRuntime(5086):     at android.app.Activity.performCreate(Activity.java:4465)
02-12 18:29:57.568: E/AndroidRuntime(5086):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
02-12 18:29:57.568: E/AndroidRuntime(5086):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)

It's because this resource is a StateListDrawable, not a Bitmap.这是因为这个资源是一个 StateListDrawable,而不是一个 Bitmap。 You should use:你应该使用:

 Drawable d = getResources().getDrawable(android.R.drawable.list_selector_background);

If you're sure that it's a bitmap, cast the drawable to BitmapDrawable and get bitmap from there:如果您确定它是位图,请将 drawable 转换为 BitmapDrawable 并从那里获取位图:

Drawable currentState = d.getCurrent();
if(currentState instanceof BitmapDrawable)
    Bitmap b = ((BitmapDrawable)currentState).getBitmap();

The bitmap may be null as well.位图也可以为空。

You should use Resources.getSystem() instead of getResources您应该使用Resources.getSystem()而不是getResources

Bitmap bm = BitmapFactory.decodeResource(Resources.getSystem(), android.R.drawable.list_selector_background);

Because the resource you are trying to access is not in your own resources, but in the system's.因为您尝试访问的资源不在您自己的资源中,而是在系统的资源中。

Edit:编辑:

Next to the wrong resources, you are trying to get a drawable from an resource that is a selector (xml file).在错误的资源旁边,您正试图从作为选择器(xml 文件)的资源中获取可绘制对象。 Please get the right drawable resource :) http://androidxref.com/4.1.1/xref/frameworks/base/core/res/res/drawable/list_selector_background.xml请获取正确的可绘制资源:) http://androidxref.com/4.1.1/xref/frameworks/base/core/res/res/drawable/list_selector_background.xml

Without using BitmapFactory:不使用 BitmapFactory:

Bitmap bmp = ((BitmapDrawable) context.getResources()
             .getDrawable(R.drawable.list_selector_background)).getBitmap();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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