"Application stopped" in Android Studio, when opening images with a spinner

0

I am developing an application in Android studio where in a spinner saved images are saved in the phone gallery, try using the uri and convert them to Bitmap, but when I give the button that would save the uri in an array the application will stop, here is the code:

public class Main2Activity extends AppCompatActivity {
public List<String> lista2;
EditText nombre;
ImageView imagen2;
Spinner combo;
Button btn1, btn2;
private static final int PICK_IMAGE = 100;
Uri imageUri;
String valor, valor2, a;
public Integer number;
ArrayAdapter<String> adaptador1;
List<String> lista1 = Arrays.asList("uno");
Bitmap bitmap;

public static String BitmapToString(Bitmap bitmap) {
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        byte[] b = baos.toByteArray();
        String temp = Base64.encodeToString(b, Base64.DEFAULT);
        return temp;
    } catch (NullPointerException e) {
        return null;
    } catch (OutOfMemoryError e) {
        return null;
    }
}
public static Bitmap StringToBitmap(String encodedString) {
    try {
        byte[] encodeByte = Base64.decode(encodedString, Base64.DEFAULT);
        Bitmap bitmap = BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
        return bitmap;
    } catch (NullPointerException e) {
        e.getMessage();
        return null;
    } catch (OutOfMemoryError e) {
        return null;
    }
}

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

    lista1 = new ArrayList<>();
    lista2 = new ArrayList<>();
    nombre = (EditText) findViewById(R.id.nombre);
    combo = (Spinner) findViewById(R.id.spinner);
    imagen2 = (ImageView) findViewById(R.id.imageView2);
    btn1 = (Button) findViewById(R.id.button3);
    btn2 = (Button) findViewById(R.id.button4);




    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
            startActivityForResult(gallery, PICK_IMAGE);

        }
    });



    btn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            valor = nombre.getText().toString();
            String valor = nombre.getText().toString();
            lista1.add(valor);
            adaptador1 = new ArrayAdapter<>(Main2Activity.this, android.R.layout.simple_spinner_dropdown_item, lista1);
            adaptador1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);


            combo.setAdapter(adaptador1);

        }
    });

    combo.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {



                Main2Activity.this.number = combo.getSelectedItemPosition() + 1;


                int i = number;
                a = lista2.get(i);

                imageUri = Uri.parse(a);

                bitmap = StringToBitmap(a);
                imagen2.setImageBitmap(bitmap);

                //imagen2.setImageURI(imageUri);
        }

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

        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    if(resultCode == RESULT_OK && requestCode == PICK_IMAGE){
        imageUri = data.getData();


        //valor2 = imageUri.toString();
        try {
            bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
        } catch (IOException e) {
            e.printStackTrace();
        }
        valor2 = BitmapToString(bitmap);

        lista2.add(valor2);

    }
}

}

    
asked by sebas-maddox 04.10.2018 в 10:21
source

0 answers