0

Estoy desarrolado una aplicacion en Android studio donde en un spinner se guardan imagenes buscadas en la galeria del telefono, lo intente usando el uri y convertinedolas a Bitmap , pero cuando le doy al boton que guardaria el uri en un array la aplicacion se detiene, aqui esta el codigo:

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

    }
}

}

4
  • 1
    Necesitariamos saber el error que te da para poder buscar algo más concreto. el 4 oct. 2018 a las 8:45
  • El error que te da en el debug o logcat? el 4 oct. 2018 a las 13:42
  • La aplicación se detiene cuando la tengo instalda en el celular, en cuanto presionó el botón que debería guardar la información en ambos arrays se detiene la aplicación el 4 oct. 2018 a las 14:31
  • Amigo debes incluir el error que te muestra el Logcat ya que ahí se encuentran pistas de por qué ocurrió el fallo además de las líneas donde ocurre.
    – Lalodev
    el 4 oct. 2018 a las 18:23

0

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

Examina otras preguntas con la etiqueta o formula tu propia pregunta.