How to pass to the function which imageview was selected and save in Firebase

0

I have a User Registration Activity where it must fill in some fields (name, gender, age, etc.) and two ImageView where the user can put pictures of him by clicking ImageView and selecting them in the gallery.

I have the method selectedPhoto set to onClick in the two ImageView, after the user chooses the photo in the onActivityResult, it takes the selected photo and calls the setImage function to put the photo in the ImageView. I want the selected photo to appear in ImageView that it clicked and that's where the problem starts.

How do I step to the ImageView function that the user clicked so that it is placed in the correct ImageView?

How to save images in Firebase Storage and Database, so you can access them in another activity (url)?

Follow the code below:

public class Anunciante extends AppCompatActivity {

    StorageReference storageRef;
    DatabaseReference databaseUsuario;

    private ImageView imgPrincipal, img02;
    private EditText txtNome, txtValor, txtDescricao, txtFone;
    private Spinner spIdade, spSexo;
    private Uri imgUri, imgUri2;

public static final String FB_STORAGE_PATH = "imagem/";
public static final String FB_DATABASE_PATH = "Anuncios";
public static final int REQUEST_CODE_01 = 1910;
public static final int REQUEST_CODE_02 = 2015;

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


    storageRef = getStorage();
    databaseUsuario = FirebaseConfig.getFirebase();

    imgPrincipal = (ImageView) findViewById(R.id.imgPrincipalAnuncio);
    imgPrincipal.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(intent.createChooser(intent, "Selecione sua foto!"), REQUEST_CODE_01);

        }
    });
    img02 = (ImageView) findViewById(R.id.img02);
    img02.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(intent.createChooser(intent, "Selecione sua foto!"), REQUEST_CODE_02);

        }
    });

    txtNome = (EditText) findViewById(R.id.txtNome);
    txtValor = (EditText) findViewById(R.id.txtValor);
    txtFone = (EditText) findViewById(R.id.txtFone);
    txtDescricao = (EditText) findViewById(R.id.txtDescricao);
    spIdade = (Spinner) findViewById(R.id.spIdade);
    spSexo = (Spinner) findViewById(R.id.spSexo);

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_CODE_01 && resultCode == RESULT_OK && data != null && data.getData() != null) {
        imgUri = data.getData();
        try{
            Bitmap bm = MediaStore.Images.Media.getBitmap(getContentResolver(), imgUri);
            imgPrincipal.setImageBitmap(bm);
        } catch (Exception e){
            e.printStackTrace();
        }
    }
    if (requestCode == REQUEST_CODE_02 && resultCode == RESULT_OK && data != null && data.getData() != null) {
        imgUri2 = data.getData();
        try{
            Bitmap bm = MediaStore.Images.Media.getBitmap(getContentResolver(), imgUri2);
            img02.setImageBitmap(bm);
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}

public String getExtImagem(Uri uri){
    ContentResolver contentResolver = getContentResolver();
    MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
    return mimeTypeMap.getExtensionFromMimeType(contentResolver.getType(uri));
}

@SuppressWarnings("VisibleForTests")
public void salvaDados(View v){

    if (imgUri != null){
        StorageReference reference = getStorage().child(FB_STORAGE_PATH + FirebaseConfig.getFirebaseUser().getUid() + "principal"+"." + getExtImagem(imgUri));
        reference.putFile(imgUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>()  {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                Upload upload = new Upload(taskSnapshot.getDownloadUrl().toString().trim(),
                        taskSnapshot.getDownloadUrl().toString().trim(), txtNome.getText().toString(),
                        spIdade.getSelectedItem().toString()+" anos", spSexo.getSelectedItem().toString(),
                        "R$ " + txtValor.getText().toString()+",00",
                        txtFone.getText().toString(), txtDescricao.getText().toString());
                String uid = FirebaseConfig.getFirebaseUser().getUid();
                databaseUsuario.child(uid).setValue(upload);

            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(getApplicationContext(), e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
            }
        }).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {

            @Override
            public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
            }
        });
    }
    goMainScreen();
}



public void goMainScreen() {
    Intent main = new Intent(this, MainActivity.class);
    startActivity(main);
    finish();
}

}

    
asked by anonymous 29.08.2017 / 14:05

1 answer

1

You can replace ImageView with an ImageButton, it provides click support.

And using your public void selecionaFoto(View v) method to be the onClick of the ImageButton, the View parameter v should provide which ImageButton was clicked.

    
29.08.2017 / 14:20