spinner change image cause error: Out of Memory on a byte allocation

Asked

Viewed 120 times

1

I have a normal spinner working perfect and I have the following code :

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.Image;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;


public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener {

    private ArrayAdapter<String> adapter;
    public ImageView img;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Spinner spinner = (Spinner) findViewById(R.id.spinner);
        adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item);
        adapter.add("A");
        adapter.add("B");
       
        spinner.setAdapter(adapter);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setOnItemSelectedListener(this);
        img = (ImageView) findViewById(R.id.imgView);
        img.setImageResource(R.drawable.imgFundo);

    }

    

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        
        String escolhido = adapter.getItem(position);
        if(escolhido == "A") {

            img.setImageResource(R.drawable.imga);
            Toast.makeText(this, "entrou no if ", Toast.LENGTH_SHORT).show();


        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        
    }
}

Obs : Images are in png
But that code isn’t working...

LOGCAT : 15:02:00.790 22578-22578/ A/libc: Fatal Signal 11 (SIGSEGV) at 0x00000000 (code=1), thread 22578

11-05 10:07:44.042 17694-17694/ E/dalvikvm-heap: Out of memory on a 9288016-byte allocation.

It does not change as required. How can I arrange?

  • Good evening Augusto, where is the Logcat? It can be dump of memory.

  • Guilhermenascimento previously believed that the problem was an error because the img was large, but when analyzing the logcat I saw that it was a memory error 11-05 10:07:44.042 17694-17694/ E/dalvikvm-heap: Out of memory on a 9288016-byte allocation. How can I fix ?

  • As suspected dump of memory, I will formulate an answer, I think I saw the same problem somewhere, already return.

  • OK obg I’m on hold

2 answers

2


I’m not sure if this is the problem, but according to this answer from Soen:

This mistake Out of memory on a 9288016-byte allocation that there was a memory leak. You’re trying to allocate ~8MB of memory, and you don’t have that much enough memory available.

It is likely that you are trying to load a Source larger than ~8mb, and in this case can be a Resource. ~ 8MB would be a truly huge image.

I would recommend that you find out what resources drawable are these then reduce their size and similar image sizes.

When I say "size" I mean in pixels and not the disk size, as what matters is the uncompressed size in RAM and not how large the image is on disk.

Also, this may be a "Resources" directory issue. For example, if you put your 1200x1200 in the briefcase res/drawable/, when the broker should be res/drawable-mdpi/.

If this device becomes a device -xhdpi, then that would explain the use of memory.

You should add the images by density in their respective folders:

  • ldpi (low) ~120dpi
  • mdpi (medium) ~160dpi
  • hdpi (high) ~240dpi
  • xhdpi (extra-high) ~320dpi
  • xxhdpi (extra-extra-high) ~480dpi
  • xxxhdpi (extra-extra-extra-high) ~640dpi

Assuming your image is about 480dpi, then it should go in this folder for example:

  • res/drawable-xxhdpi/

For every new density you need, you will need to create a folder like this res/drawable-[densidade]/ (changing the [densidade] by the amount of dpi the image has).

Source: http://developer.android.com/guide/practices/screens_support.html

  • friend is right, decrease the px and it worked I am with 458 1000 , which the maximum I can use ?

  • It will depend on the Source folder you have placed, I will edit the @Augustofurlan question as soon as possible and put these details.

  • OK, I’m putting everyone inside the drawable , how could put good images ( but I want to cover various devices ... )

  • @Augustofurlan I edited the answer, see if it helps to understand.

  • I am seeing, very obg , sorry my igonorancia, as I see the dpis , because hj only see px ...

  • If it’s windows, right-click on the image, I think it says the dpis of the selected photo, I’m not sure. @Augustofurlan

  • ok obg helped me a lot friend

Show 2 more comments

0

Try it like this:

 if("A".equals(escolha)) {
            ImageView imgf = (ImageView) findViewById(R.id.mapView);
            imgf.setImageResource(R.drawable.img_a);

   }

When using == compare if the reference is equal.

And when we use equals, we compare the value !

to learn more: How to compare Strings in Java?

  • Friend, with the == it enters the if, because I put a Toast to see and displayed that entered the if, it is not changing the image ....

  • it runs no more swap the image? in xml where Voce creates the image, has something set in src?

  • Exactly do not exchange image ...

  • No , not set in , I put an image with that same code in onCreate

  • Then try using the same onCreate instance. Create Imageview as a global variable !

  • How do you set to var global ? I tried using onecreate instance and it also didn’t work ...

  • Create Imageview in the scope of the class, not within any method, then in onCreate you arrow its value: (Imageview) findViewById(R.id.mapView);

  • I did it, it hasn’t worked yet ... Hahsha ta dificil...

  • Post q VC did! It’s easier to understand

  • Buddy, I just entered the full code

  • friend even put the logcat

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.