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

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) {

    }
}

Note: Images are in png
But this code is not 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 fix it?

    
asked by anonymous 30.10.2015 / 19:41

2 answers

2

I'm not sure if this is the problem, but depending on this response from SOen :

This error Out of memory on a 9288016-byte allocation to say that there was a memory leak. You are trying to allocate ~ 8MB of memory, and you do not have that much memory available.

It is likely that you are attempting to load a resource greater 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 drawable features these are, then reduce their size and similar image sizes.

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

In addition, this can be a problem for directories of "resources". For example, if you put your image of 1200x1200 in the res/drawable/ folder, when the broker should be res/drawable-mdpi/ .

If this device becomes a -xhdpi device, then this would explain the memory usage.

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 approximately 480dpi, then it should go in this folder for example:

  • res/drawable-xxhdpi/

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

Source: link

    
05.11.2015 / 14:04
0

Try this:

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

   }

When using == we compare if the reference is equal.

And when we use equals, we compare the value!

To learn more: How to Compare Strings in Java?

    
30.10.2015 / 19:49