2013-02-12 10 views
7

Ciao sto facendo questo utilizzando questo modoCome ottenere Bitmap dalla risorsa predefinita di Android?

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

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

ma BM è null come dà NullPointerException in seconda linea.

Errore:

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) 

risposta

5

È perché questa risorsa è una StateListDrawable, non una bitmap. Si dovrebbe usare:

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

Se sei sicuro che si tratta di una bitmap, il cast del drawable per BitmapDrawable e ottenere bitmap da lì:

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

La bitmap può essere nullo pure.

+0

puoi dirmi, Come posso ottenere tutto ciò che è drawable in StateListDrawable. come usando getState() sto sempre ricevendo int [] con dimensione 0. –

+1

Per ottenere un drawable per qualsiasi stato specifico, utilizzare getStateDrawableIndex e getStateDrawable in questo StateListDrawable. getState restituisce la lista degli stati attualmente applicati a questo StateListDrawable. Vedi: [link] (http://developer.android.com/reference/android/graphics/drawable/StateListDrawable.html) – Zielony

+0

E come si ottiene ** Bitmap ** da questo? –

3

Si dovrebbe usare Resources.getSystem() invece di getResources

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

Perché la risorsa che si sta tentando di accedere non è nelle vostre risorse proprie, ma nel sistema di.

Edit:

Accanto alle risorse sbagliate, si sta cercando di ottenere un drawable da una risorsa che è un selettore (file XML). Si prega di ottenere la giusta risorsa disegnabile :) http://androidxref.com/4.1.1/xref/frameworks/base/core/res/res/drawable/list_selector_background.xml

+0

ancora lo stesso errore –

+0

risposta Aggiornato .. –

+0

@lon Aalbers era buon aiuto per me, ma mi puoi dire, Come posso ottenere tutto ciò che drawable che sono in StateListDrawable. come usando getState() sto sempre ricevendo int [] con dimensione 0. –

4

Senza usare BitmapFactory:

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