Autore Topic: java.lang.OutOfMemoryError: bitmap size exceeds VM budget  (Letto 779 volte)

Offline Spagna

  • Utente junior
  • **
  • Post: 81
  • Respect: 0
    • Mostra profilo
  • Dispositivo Android:
    Samsung Galaxy
  • Sistema operativo:
    windows 7
java.lang.OutOfMemoryError: bitmap size exceeds VM budget
« il: 18 Gennaio 2013, 12:14:45 CET »
0
Buongiorno a tutti,
Ho adattato un tutoriale  un GridView che si alimenta di imagini in un Server Remoto.

Ho creato il "Adapter" y una Class che importa le imagini:

GridviewConfig:
Codice (Java): [Seleziona]
import java.util.ArrayList;

import android.util.Log;

public class GridViewConfig {
        private static ArrayList<String> resim_list=new ArrayList<String>();
       
        public static ArrayList<String> getResim_list() {
                return resim_list;
        }

        public static void setResim_list(ArrayList<String> resim_list) {
                GridViewConfig.resim_list = resim_list;
                Log.e("SetResim :", "pro");;
        }

        public static void addImageUrls(){
                Log.e("AddImagen :", "pro");;
                resim_list.add("http://www.midominio.com/carlo/img/uno.jpg");
                resim_list.add("http://www.midominio.com/carlo/img/dos.jpg");
                resim_list.add("http://www.midominio.com/carlo/img/tres.jpg");
                resim_list.add("http://www.midominio.com/carlo/img/cuatro.jpg");
                resim_list.add("http://www.midominio.com/carlo/img/cinco.jpg");
                resim_list.add("http://www.midominio.com/carlo/img/seis.jpg");
               
        }
}
ImageAdptador:
Codice (Java): [Seleziona]
import java.io.InputStream;
import java.net.URI;
import java.net.URL;

import android.R.integer;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListAdapter;

public class ImageAdapter extends BaseAdapter implements ListAdapter {

        private Context context;
        public ImageAdapter(Context context) {
                super();
                this.context = context;
//Listeye image url si ekliyor
                GridViewConfig.addImageUrls();
        }

       
        @Override
        public int getCount() {
                return GridViewConfig.getResim_list().size();
        }

        @Override
        public Object getItem(int position) {

                return GridViewConfig.getResim_list().get(position);
        }

        @Override
        public long getItemId(int position) {
               
                return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                ImageView imageView;
                Log.e("Getview :", "pro");;
                if(convertView==null)
                {
                        imageView=new ImageView(context);
                        imageView.setLayoutParams(new GridView.LayoutParams(100,100));
                        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                        imageView.setPadding(5,5,5,5);
                }else{
                        imageView=(ImageView)convertView;
                }
                imageView.setImageDrawable(LoadImageFromURL(GridViewConfig.getResim_list().get(position)));
                return imageView;
        }
       
//Internetten imageleri stream olarak  cekip drawable olsurturuyor.
        private Drawable LoadImageFromURL(String url)
        {
                Log.e("LoadImagen :", "pro");;
        try
        {
        InputStream is = (InputStream) new URL(url).getContent();
        Drawable d = Drawable.createFromStream(is, "src");
        return d;
        }catch (Exception e) {
        System.out.println(e);
        return null;
        }
        }
       

}

La provo e mi da error:
Codice (Java): [Seleziona]
01-18 11:58:45.207: D/dalvikvm(404): GC_EXTERNAL_ALLOC freed 67K, 52% free 2606K/5379K, external 903K/1038K, paused 61ms
01-18 11:58:46.077: E/Getview :(404): pro
01-18 11:58:46.077: E/LoadImagen :(404): pro
01-18 11:58:46.307: D/dalvikvm(404): GC_EXTERNAL_ALLOC freed 49K, 52% free 2606K/5379K, external 3975K/4964K, paused 53ms
01-18 11:58:47.077: E/Getview :(404): pro
01-18 11:58:47.077: E/LoadImagen :(404): pro
01-18 11:58:47.297: D/dalvikvm(404): GC_EXTERNAL_ALLOC freed 48K, 52% free 2606K/5379K, external 7047K/8800K, paused 56ms
01-18 11:58:48.137: E/Getview :(404): pro
01-18 11:58:48.137: E/LoadImagen :(404): pro
01-18 11:58:49.097: E/Getview :(404): pro
01-18 11:58:49.097: E/LoadImagen :(404): pro
01-18 11:58:49.317: D/dalvikvm(404): GC_EXTERNAL_ALLOC freed 96K, 52% free 2605K/5379K, external 10119K/10119K, paused 57ms
01-18 11:58:49.387: E/dalvikvm-heap(404): 3145728-byte external allocation too large for this process.
01-18 11:58:49.497: I/dalvikvm-heap(404): Clamp target GC heap from 16.970MB to 16.000MB
01-18 11:58:49.497: E/GraphicsJNI(404): VM won't let us allocate 3145728 bytes
01-18 11:58:49.497: D/dalvikvm(404): GC_FOR_MALLOC freed <1K, 52% free 2605K/5379K, external 10119K/10119K, paused 39ms
01-18 11:58:49.497: D/skia(404): --- decoder->decode returned false
01-18 11:58:49.507: D/AndroidRuntime(404): Shutting down VM
01-18 11:58:49.507: W/dalvikvm(404): threadid=1: thread exiting with uncaught exception (group=0x40015560)
01-18 11:58:49.527: E/AndroidRuntime(404): FATAL EXCEPTION: main
01-18 11:58:49.527: E/AndroidRuntime(404): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
01-18 11:58:49.527: E/AndroidRuntime(404):      at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
01-18 11:58:49.527: E/AndroidRuntime(404):      at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:470)
01-18 11:58:49.527: E/AndroidRuntime(404):      at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
01-18 11:58:49.527: E/AndroidRuntime(404):      at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
01-18 11:58:49.527: E/AndroidRuntime(404):      at android.graphics.drawable.Drawable.createFromStream(Drawable.java:657)
01-18 11:58:49.527: E/AndroidRuntime(404):      at com.gridview.sample.ImageAdapter.LoadImageFromURL(ImageAdapter.java:70)
01-18 11:58:49.527: E/AndroidRuntime(404):      at com.gridview.sample.ImageAdapter.getView(ImageAdapter.java:59)
01-18 11:58:49.527: E/AndroidRuntime(404):      at android.widget.AbsListView.obtainView(AbsListView.java:1430)
01-18 11:58:49.527: E/AndroidRuntime(404):      at android.widget.GridView.makeAndAddView(GridView.java:1222)
01-18 11:58:49.527: E/AndroidRuntime(404):      at android.widget.GridView.makeRow(GridView.java:268)
01-18 11:58:49.527: E/AndroidRuntime(404):      at android.widget.GridView.fillDown(GridView.java:221)
01-18 11:58:49.527: E/AndroidRuntime(404):      at android.widget.GridView.fillFromTop(GridView.java:340)
01-18 11:58:49.527: E/AndroidRuntime(404):      at android.widget.GridView.layoutChildren(GridView.java:1136)
01-18 11:58:49.527: E/AndroidRuntime(404):      at android.widget.AbsListView.onLayout(AbsListView.java:1260)
01-18 11:58:49.527: E/AndroidRuntime(404):      at android.view.View.layout(View.java:7175)
01-18 11:58:49.527: E/AndroidRuntime(404):      at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
01-18 11:58:49.527: E/AndroidRuntime(404):      at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
01-18 11:58:49.527: E/AndroidRuntime(404):      at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
01-18 11:58:49.527: E/AndroidRuntime(404):      at android.view.View.layout(View.java:7175)
01-18 11:58:49.527: E/AndroidRuntime(404):      at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
01-18 11:58:49.527: E/AndroidRuntime(404):      at android.view.View.layout(View.java:7175)
01-18 11:58:49.527: E/AndroidRuntime(404):      at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
01-18 11:58:49.527: E/AndroidRuntime(404):      at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
01-18 11:58:49.527: E/AndroidRuntime(404):      at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
01-18 11:58:49.527: E/AndroidRuntime(404):      at android.view.View.layout(View.java:7175)
01-18 11:58:49.527: E/AndroidRuntime(404):      at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
01-18 11:58:49.527: E/AndroidRuntime(404):      at android.view.View.layout(View.java:7175)
01-18 11:58:49.527: E/AndroidRuntime(404):      at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
01-18 11:58:49.527: E/AndroidRuntime(404):      at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
01-18 11:58:49.527: E/AndroidRuntime(404):      at android.os.Handler.dispatchMessage(Handler.java:99)
01-18 11:58:49.527: E/AndroidRuntime(404):      at android.os.Looper.loop(Looper.java:123)
01-18 11:58:49.527: E/AndroidRuntime(404):      at android.app.ActivityThread.main(ActivityThread.java:3683)
01-18 11:58:49.527: E/AndroidRuntime(404):      at java.lang.reflect.Method.invokeNative(Native Method)
01-18 11:58:49.527: E/AndroidRuntime(404):      at java.lang.reflect.Method.invoke(Method.java:507)
01-18 11:58:49.527: E/AndroidRuntime(404):      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-18 11:58:49.527: E/AndroidRuntime(404):      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-18 11:58:49.527: E/AndroidRuntime(404):      at dalvik.system.NativeStart.main(Native Method)
01-18 11:58:51.680: I/Process(404): Sending signal. PID: 404 SIG: 9
Devo redurre automaticamente le imagini
Non so come farlo
Un saluto
« Ultima modifica: 18 Gennaio 2013, 12:55:17 CET da Spagna »

Offline elfo83

  • Utente normale
  • ***
  • Post: 287
  • Respect: +23
    • Mostra profilo
  • Sistema operativo:
    Mac OS 10.8.2
Re:java.lang.OutOfMemoryError: bitmap size exceeds VM budget
« Risposta #1 il: 18 Gennaio 2013, 23:35:33 CET »
+1
Ti da quell'errore perchè le immagini che carichi occupano troppa memoria..ti conviene seguire questa guida

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

Offline Sakazaki

  • Utente normale
  • ***
  • Post: 396
  • Respect: +74
    • Mostra profilo
  • Dispositivo Android:
    Sony xperia Z
  • Play Store ID:
    Saka Labs
  • Sistema operativo:
    Windows 8
Re:java.lang.OutOfMemoryError: bitmap size exceeds VM budget
« Risposta #2 il: 21 Gennaio 2013, 08:53:46 CET »
0
Ti da quell'errore perchè le immagini che carichi occupano troppa memoria..ti conviene seguire questa guida

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
Quest'articolo della documentazione mi era completamente sfuggito...
Grazie mille per la segnalazione!  ;-)

Offline Spagna

  • Utente junior
  • **
  • Post: 81
  • Respect: 0
    • Mostra profilo
  • Dispositivo Android:
    Samsung Galaxy
  • Sistema operativo:
    windows 7
Re:java.lang.OutOfMemoryError: bitmap size exceeds VM budget
« Risposta #3 il: 21 Gennaio 2013, 11:14:31 CET »
0
Grazie
UN saluto
a la prosima