2013-02-12 44 views
7

嗨,我使用這種方式如何從android的默認資源獲取位圖?

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

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

但BM是null作爲第二行給出NullPointerException這樣做。

錯誤:

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) 

回答

5

這是因爲此資源是StateListDrawable,而不是位圖。您應該使用:

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

如果你確定這是一個位圖,投提拉到BitmapDrawable並從那裏得到的位圖:

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

位圖可以爲空也。

+0

你能告訴我,我怎樣才能得到所有在StateListDrawable中繪製的。作爲使用getState()我總是得到0尺寸的int []。 – 2013-02-13 05:52:14

+1

要獲取任何特定狀態的drawable,請在此StateListDrawable上使用getStateDrawableIndex和getStateDrawable。 getState返回當前應用於此StateListDrawable的狀態列表。請參閱:[鏈接](http://developer.android.com/reference/android/graphics/drawable/StateListDrawable.html) – Zielony 2013-02-15 10:51:55

+0

然後你怎麼從這個**位圖**? – 2016-03-03 15:17:55

3

您應該使用Resources.getSystem()代替getResources

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

因爲您試圖訪問該資源不是在自己的資源,但在系統的。

編輯:

下一頁到錯誤的資源,你正試圖從一個選擇器(XML文件)的資源得到繪製。請獲取正確的可繪製資源:) http://androidxref.com/4.1.1/xref/frameworks/base/core/res/res/drawable/list_selector_background.xml

+0

還是同樣的錯誤 – 2013-02-12 13:43:36

+0

更新答案.. – 2013-02-12 13:53:01

+0

@lon Aalbers對我來說是很好的幫助,但你能告訴我,我怎樣才能得到所有的繪製這是在StateListDrawable。作爲使用getState()我總是得到0尺寸的int []。 – 2013-02-13 05:51:19

4

不使用BitmapFactory:

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