2013-07-24 10 views
14

ho bisogno di ottenere la larghezza e l'altezza di una bitmap, ma utilizzando questo dà un'eccezione di memoria:Android - la larghezza e l'altezza del bitmap senza caricarla

Resources res=getResources(); 
    Bitmap mBitmap = BitmapFactory.decodeResource(res, R.drawable.pic); 
    BitmapDrawable bDrawable = new BitmapDrawable(res, mBitmap); 

    //get the size of the image and the screen 
    int bitmapWidth = bDrawable.getIntrinsicWidth(); 
    int bitmapHeight = bDrawable.getIntrinsicHeight(); 

ho letto la soluzione alla domanda Get bitmap width and height without loading to memory ma quale sarebbe l'inputStream qui?

+0

dal ur immagine è grande per caricare in memoria – KOTIOS

+0

Ho pensato che troppo .. Di qui la domanda come ottenere le metriche senza caricare in memoria – Gooey

risposta

15

è necessario specificare alcuni BitmapFactory.Options così:

BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inJustDecodeBounds = true; 
BitmapFactory.decodeResource(getResources(), R.id.myimage, options); 
int imageHeight = options.outHeight; 
int imageWidth = options.outWidth; 

bDrawable non conterrà qualsiasi array di byte bitmap. Tratto da here: Setting the inJustDecodeBounds property to true while decoding avoids memory allocation, returning null for the bitmap object but setting outWidth, outHeight and outMimeType. This technique allows you to read the dimensions and type of the image data prior to construction (and memory allocation) of the bitmap.

+2

decodeResource restituirà null, se options.inJustDecodeBounds = true; Quindi puoi omettere l'assegnazione 'Bitmap bDrawable = BitmapFactory.de'. Anche tu rispondi è sbagliato. devi ottenere widht e heigh dalle opzioni – Blackbelt

Problemi correlati