2014-06-11 7 views
10

questo è in relazione a questa domandadi eccezione di memoria + file di hprof analizzare il dump

java.lang.OutOfMemoryError at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)

ho creato il file di dump in questione .. e dà seguenti informazioni

One instance of "byte[]" loaded by "<system class loader>" occupies 1,10,59,216 
(51.02%) bytes. The memory is accumulated in one instance of "byte[]" 
loaded by "<system class loader>". 

Keywords byte[] 

così ora cosa si può fare ?? come posso risolvere il problema ??

miei list_objects [contesto] File -inbound

CLASS NAME                 SHALLOW HEAP RETAINED HEAP 
byte[11059200] @ 0xb4979590            | 1,10,59,216 | 1,10,59,216 
mBuffer android.graphics.Bitmap @ 0xb3dc68d8        |48   | 48 
mBitmap android.graphics.drawable.BitmapDrawable @ 0xb3dbba60    | 72   | 144 
mBackground android.widget.RelativeLayout @ 0xb3db3fc0     |512   | 10,144 
mBitmap android.graphics.drawable.BitmapDrawable$BitmapState @ 0xb3dc0068 |40   | 40 
mBitmapState android.graphics.drawable.BitmapDrawable @ 0xb3dbba60  |72   | 144 
referent java.lang.ref.WeakReference @ 0xb3dc2d68       |24   | 24 

pls aiuto io sono disperata. come posso risolvere il problema della memoria ??

mia home_screen.java

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.home_page); 
    main(); 
private void main() { 
    // TODO Auto-generated method stub 


    final Button home; 
    final Button aboutus; 
    final Button contacts; 
    final Button clients; 
    final Button services; 

    try 
    { 

    home = (Button)findViewById(R.id.btnHome); 
    aboutus = (Button)findViewById(R.id.btnAboutus); 
    clients = (Button)findViewById(R.id.btnClients); 
    contacts = (Button)findViewById(R.id.btnContacts); 
    services = (Button)findViewById(R.id.btnServices); 

    home.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Bitmap b = BitmapFactory.decodeResource(getResources(),R.drawable.home1); 
      Drawable d = new BitmapDrawable(getResources(),b);    
      home.setBackgroundDrawable(d); 
      System.gc(); 
      Intent myIntent = new Intent(Home_Screen.this, Button_Anime.class); 
      startActivity(myIntent); 
     } 
    }); 
    aboutus.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Bitmap b = BitmapFactory.decodeResource(getResources(),R.drawable.about1); 
      Drawable d = new BitmapDrawable(getResources(),b); 
      aboutus.setBackgroundDrawable(d); 
      Intent myIntent = new Intent(Home_Screen.this, AboutUs.class); 
      startActivity(myIntent); 
     } 
    }); 
    clients.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      Bitmap b = BitmapFactory.decodeResource(getResources(),R.drawable.clients1); 
      Drawable d = new BitmapDrawable(getResources(),b); 
      clients.setBackgroundDrawable(d); 
      Intent myIntent = new Intent(Home_Screen.this, Clients.class); 
      startActivity(myIntent); 
     } 
    }); 
    contacts.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      Bitmap b = BitmapFactory.decodeResource(getResources(),R.drawable.contact1); 
      Drawable d = new BitmapDrawable(getResources(),b); 
      contacts.setBackgroundDrawable(d); 
      Intent myIntent = new Intent(Home_Screen.this, Contacts.class); 
      startActivity(myIntent); 
     } 
    }); 
    services.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      Bitmap b = BitmapFactory.decodeResource(getResources(),R.drawable.services1); 
      Drawable d = new BitmapDrawable(getResources(),b); 
      services.setBackgroundDrawable(d); 
      Intent myIntent = new Intent(Home_Screen.this, Services.class); 
      startActivity(myIntent); 
     } 
    }); 

    } 
    catch(Exception e) 
    { 
     e.printStackTrace(); 
    } 
} 
+0

incollare il code.so che abbiamo wil analizziamo. – Dev

+0

@DevCarlsberg ho postato il mio file principale .. pls controllare .. – user3214173

+0

Dude passare attraverso [My this Answer] (http://stackoverflow.com/questions/24156047/android-binary-xml-exception-when-changing-pictures/24157195 # 24157195) ti aiuterà. – Dev

risposta

1

provare il codice qui sotto:

Resources res = getContext().getResources(); 
int id = R.drawable.image; 
Bitmap b = BitmapFactory.decodeResource(res, id);  
_img .setimagebitmap(b); 
+0

@DevCalsberg questo codice non funziona eclipse dosnt accettarlo .. Risorse res = getContext(). getResources(); home.setimagebitmap (b); entrambi questi codici producono errore .. e inoltre questo codice non è logicamente uguale a quello che ho usato ??? – user3214173

1

Normalmente quando ottengo questo tipo di eccezione su dispositivi a basso Ram è perché ho qualche tipo di immagine a costosi (molti kb) per questa risoluzione. Ho avuto un problema strano perché ho appena usato le directory xhdpi e hdpi drawable, su dispositivi mdpi o anche ldpi la conversione hdpi alla giusta risoluzione solleva questa eccezione perché non è possibile ridimensionare l'immagine.

Succede su qualsiasi dispositivo? Potresti controllare di avere una grafica specifica per questi dispositivi bassi?

In qualsiasi modo con il file HPOF è possibile vedere quale tipo di oggetto o almeno, quale attività/vista ha il problema. È necessario analizzare tutti questi dati (non esiste un piano principale per trovare la risorsa che sta comprimendo la tua app) per vedere quale ha il problema.

Spero che questo aiuti, qualsiasi domanda, non esitate a chiedere.

4

L'idea è di campionare l'immagine in modo che appaia bene sullo schermo più piccolo e che non sia necessario caricare l'intera bitmap in memoria.

1) In primo luogo ottenere le dimensioni del tuo ImageView/schermo che verrà visualizzato.

2) Leggere le dimensioni di Bitmap passando BitmapFactory.Options.inJustDecodeBounds. Questo ti darà la dimensione della Bitmap anziché caricare l'intera bitmap.

3) Ottieni una dimensione di esempio. Calcola il rapporto tra altezza e larghezza dello schermo per l'altezza e la larghezza dell'immagine. Usa il più piccolo in modo che la dimensione più grande appaia buona.

4) Utilizzare la funzione seguente per ottenere l'immagine campionata che non consuma la memoria.

2) (codice)

BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); 
bitmapOptions.inJustDecodeBounds = true; 
BitmapFactory.decodeStream(inputStream, null, bitmapOptions); 
int imageWidth = bitmapOptions.outWidth; 
int imageHeight = bitmapOptions.outHeight; 
inputStream.close(); 

4) (codice)

private Bitmap downscaleBitmapUsingDensities(final int sampleSize,final int imageResId) 
    { 
    final Options bitmapOptions=new Options(); 
    bitmapOptions.inDensity=sampleSize; 
    bitmapOptions.inTargetDensity=1; 
    final Bitmap scaledBitmap=BitmapFactory.decodeResource(getResources(),imageResId,bitmapOptions); 
    scaledBitmap.setDensity(Bitmap.DENSITY_NONE); 
    return scaledBitmap; 
    } 
2

Aggiunta largeHeap = "true" nella vostra manifesta può aiutare. Ciò consentirà alla tua applicazione di utilizzare più memoria.

See: http://developer.android.com/guide/topics/manifest/application-element.html

+0

Questo è un bandaid nella migliore delle ipotesi, dal link: "La maggior parte delle app non dovrebbe averne bisogno e dovrebbe invece concentrarsi sulla riduzione dell'uso complessivo della memoria." – Sully

Problemi correlati