2014-06-24 12 views
22

Ho esaminato più domande simili, anche se non ho trovato risposta adeguata sulla mia query.BitmapFactory.decodeResource() restituisce null per la forma definita in xml drawable

Ho un disegnabile, definito in shape.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > 

    <solid android:color="@color/bg_color" /> 
</shape> 

voglio convertirlo in oggetto bitmap per eseguire alcune operazioni, ma BitmapFactory.decodeResource() restituisce null.

Ecco come lo sto facendo:

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.shape); 

Che cosa sto facendo di sbagliato? BitmapFactory.decodeResource() applicabile per i drawable definiti xml?

risposta

31

Dal momento che si desidera caricare un Drawable, non un Bitmap, utilizzare questo:

Drawable d = getResources().getDrawable(...); 

per trasformarlo in un Bitmap:

public static Bitmap drawableToBitmap (Drawable drawable) { 

    if (drawable instanceof BitmapDrawable) { 
     return ((BitmapDrawable)drawable).getBitmap(); 
    } 

    Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Config.ARGB_8888); 
    Canvas canvas = new Canvas(bitmap); 
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); 
    drawable.draw(canvas); 

    return bitmap; 
} 

Tratto da: How to convert a Drawable to a Bitmap?

+4

Mi chiedo come questo può funzionare ... Dal momento che il drawable è una forma definita in XML, 'getIntrinsicWidth()' e 'getIntrinsicHeight()' restituisce sempre -1 e il won bitmap essere creato O sto ricevendo qualcosa di sbagliato? –

+3

Questo genera 'IllegalArgumentException: width e height devono essere> 0' –

+0

Ciò significa che il tuo drawable non ha una dimensione valida. –

1

si è un drawable, non un bitmap. Si dovrebbe usare getDrawable invece

+0

Non puoi usare 'getDrawable' su ** StateListDrawable ** o ** Shape ** –

1
public static Bitmap convertDrawableResToBitmap(@DrawableRes int drawableId, Integer width, Integer height) { 
    Drawable d = getResources().getDrawable(drawableId); 

    if (d instanceof BitmapDrawable) { 
     return ((BitmapDrawable) d).getBitmap(); 
    } 

    if (d instanceof GradientDrawable) { 
     GradientDrawable g = (GradientDrawable) d; 

     int w = d.getIntrinsicWidth() > 0 ? d.getIntrinsicWidth() : width; 
     int h = d.getIntrinsicHeight() > 0 ? d.getIntrinsicHeight() : height; 

     Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); 
     Canvas canvas = new Canvas(bitmap); 
     g.setBounds(0, 0, w, h); 
     g.setStroke(1, Color.BLACK); 
     g.setFilterBitmap(true); 
     g.draw(canvas); 
     return bitmap; 
    } 

    Bitmap bit = BitmapFactory.decodeResource(getResources(), drawableId); 
    return bit.copy(Bitmap.Config.ARGB_8888, true); 
} 

//------------------------ 

Bitmap b = convertDrawableResToBitmap(R.drawable.myDraw , 50, 50); 
+0

aggiungi qualche spiegazione alla tua risposta – RamPrakash

Problemi correlati