1. Nimm jetzt an unserem Uhans - 3. ADVENT - Gewinnspiel teil - Alle Informationen findest Du hier!

Speicherproblem in Gallery

Dieses Thema im Forum "Android App Entwicklung" wurde erstellt von Butch78, 19.12.2011.

  1. Butch78, 19.12.2011 #1
    Butch78

    Butch78 Threadstarter Junior Mitglied

    Beiträge:
    28
    Erhaltene Danke:
    0
    Registriert seit:
    06.12.2011
    Hi Leute,

    ich habe mir einer kleine Gallerie erstellt, in der ich gerne alle Bilder drin haben mächte, die ich in meiner App aufnehme. Das klappt auch schon alles soweit ganz gut. Derzeit habe ich nur 4 Bilder drin. 2 ganz kleine und 2 die über 1 MB groß sind. Diese machen mir aber in meiner Gallerie etwas Probleme. Und zwar, bekomme ich durch diese immer eine OutofMemoryException.
    1 MB ist ja nun noch nicht wirklich groß und 2 Bilder auch nicht gerade viel. Wie bekommt man die Speicherprobleme in den Griff?
    Oder muss ich einfach die Bilder in kleinerer Auflösung speichern?
    Die Standard Android Gallerie kann ja auch mit vielen/ großen Bildern recht performant umgehen.
    Hier mal mein Code.

    Code:
    
    [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public
    [/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]class[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ImageGallery [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]extends[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Activity {[/SIZE]
    [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]private[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Bundle [/SIZE][U][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]extras[/COLOR][/SIZE][/COLOR][/SIZE][/U][SIZE=2];[/SIZE]
    [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]private[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] File[] [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]mFileArray[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE][/LEFT]
     
    [LEFT][SIZE=2][COLOR=#646464][SIZE=2][COLOR=#646464]@Override[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#646464]
    [/COLOR][/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] onCreate(Bundle savedInstanceState) {[/SIZE]
    [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]super[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2].onCreate(savedInstanceState);[/SIZE]
    [SIZE=2]setContentView(R.layout.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]gallery[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE]
    [SIZE=2]setImageList();[/SIZE]
    [SIZE=2]Gallery gallery = (Gallery) findViewById(R.id.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]gallery1[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE][/LEFT]
     
    [LEFT][SIZE=2]gallery.setAdapter([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ImageAdapter([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]this[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2])); [/SIZE]
    [SIZE=2]gallery.setOnItemClickListener([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] OnItemClickListener() [/SIZE]
    [SIZE=2]{[/SIZE]
    [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] onItemClick([U]AdapterView[/U] parent, [/SIZE]
    [SIZE=2]View v, [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] position, [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]long[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] id) [/SIZE]
    [SIZE=2]{ [/SIZE]
    [SIZE=2]Uri uri=Uri.[I]parse[/I]([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"file:///"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]mFileArray[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][position].toString()); [/SIZE]
    [SIZE=2]ImageView imageView = (ImageView) findViewById(R.id.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]gallery_image[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]); [/SIZE]
    [SIZE=2]imageView.setImageURI(uri);[/SIZE][/LEFT]
     
    [LEFT][SIZE=2]}[/SIZE]
    [SIZE=2]});[/SIZE]
    [SIZE=2]}[/SIZE][/LEFT]
     
     
    [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]private[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] setImageList(){[/SIZE]
    [SIZE=2]File f = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] File([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"/mnt/sdcard/de.androiddev.myapp/images"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
    [SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]mFileArray[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = f.listFiles();[/SIZE]
    [SIZE=2]}[/SIZE][/LEFT]
     
    [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]class[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ImageAdapter [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]extends[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] BaseAdapter [/SIZE]
    [SIZE=2]{[/SIZE]
    [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]private[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Context [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]context[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
    [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]private[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]itemBackground[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE][/LEFT]
     
    [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ImageAdapter(Context c) [/SIZE]
    [SIZE=2]{[/SIZE]
    [SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]context[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = c;[/SIZE]
    [SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//---setting the style---[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f7f5f]
    [/COLOR][/SIZE][SIZE=2]TypedArray a = obtainStyledAttributes(R.styleable.[/LEFT]
    [/LEFT]
    [/SIZE][LEFT][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]Gallery1[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE]
    [LEFT][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]itemBackground[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = a.getResourceId([/SIZE]
    [SIZE=2]R.styleable.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]Gallery1_android_galleryItemBackground[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2], 0);[/SIZE]
    [SIZE=2]a.recycle(); [/SIZE]
    [SIZE=2]}[/SIZE][/LEFT]
     
    [LEFT][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//---returns the number of images---[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f7f5f]
    [/COLOR][/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] getCount() {[/SIZE]
    [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]return[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]mFileArray[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]length[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
    [SIZE=2]}[/SIZE][/LEFT]
     
    [LEFT][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//---returns the ID of an item--- [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f7f5f]
    [/COLOR][/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Object getItem([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] position) {[/SIZE]
    [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]return[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] position;[/SIZE]
    [SIZE=2]} [/SIZE][/LEFT]
     
    [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]long[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] getItemId([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] position) {[/SIZE]
    [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]return[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] position;[/SIZE]
    [SIZE=2]}[/SIZE][/LEFT]
     
    [LEFT][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//---returns an ImageView view---[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f7f5f]
    [/COLOR][/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] View getView([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] position, View convertView, ViewGroup parent) {[/SIZE]
    [SIZE=2]ImageView imageView = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ImageView([/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]context[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
    [SIZE=2]Uri uri=Uri.[I]parse[/I]([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"file:///"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]mFileArray[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][position].toString());[/SIZE]
    [SIZE=2]imageView.setImageURI(uri); [/SIZE]
    [SIZE=2]imageView.setScaleType(ImageView.ScaleType.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]FIT_XY[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE]
    [SIZE=2]imageView.setLayoutParams([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Gallery.LayoutParams(150, 120));[/SIZE]
    [SIZE=2]imageView.setBackgroundResource([/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]itemBackground[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
    [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]return[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] imageView;[/SIZE]
    [SIZE=2]}[/SIZE]
    [SIZE=2]} [/SIZE][/LEFT]
    [SIZE=2]}[/SIZE]
     
     
    Grüße
     
    Butch
    
    [/LEFT]
     
  2. Tom299, 19.12.2011 #2
    Tom299

    Tom299 Android-Experte

    Beiträge:
    602
    Erhaltene Danke:
    120
    Registriert seit:
    31.08.2011
    vielleicht wäre es besser, wenn du von allen bildern eigene vorschaubilder erstellen würdest, und diese dann in der liste anzeigst? und erst beim anzeigen des eigentlichen bildes dann das richtige lädst?
     

Diese Seite empfehlen