0

Estoy intentando subir fotos a firebase pero estoy notando que se estan sobre-escribiendo y siempre muestra la ultima foto subida y borra la anterior, me gustaria encontrar la solucion porque aparte de que es un problema tengo mucha curiosidad de saber porque lo hace, espero ser escuchado.

     imageView = findViewById(R.id.imageView);
    imageView.setImageDrawable(new BitmapDrawable(getResources(),Actividad3.getBitmap_transfer()));


    storageReference = FirebaseStorage.getInstance().getReference(Constantes.STORAGE_PATH_UPLOADS);
    mDatabase = FirebaseDatabase.getInstance().getReference(Constantes.DATABASE_PATH_UPLOADS);




}



}

private void uploadFile() {



    imageView.setDrawingCacheEnabled(true);
    imageView.buildDrawingCache();

    BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
    Bitmap bitmap = drawable.getBitmap();

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);

    byte[] data = baos.toByteArray();

        //Mostrar Progreso Dialogo mientras sube imagen
        final ProgressDialog progressDialog = new ProgressDialog(this);

        //progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setTitle("Subiendo foto");
        progressDialog.show(); // Lo muestra


          StorageReference mountainsRef = storageReference.child(Constantes.STORAGE_PATH_UPLOADS);

          //getting the storage reference
         UploadTask uploadTask = mountainsRef.putBytes(data);

        //adding the file to reference
        uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {


                //dismissing the progress dialog
                progressDialog.dismiss();


                Task<Uri> urlTask = taskSnapshot.getStorage().getDownloadUrl();

                while (!urlTask.isSuccessful()) ;

                Uri downloadUrl = urlTask.getResult();

                Upload upload = new Upload(downloadUrl.toString());

                String uploadId = mDatabase.push().getKey();

                mDatabase.child(uploadId).setValue(upload);


                //Mostrar mensaje si subio la imagen
                Toast.makeText(getApplicationContext(), "Foto subida", Toast.LENGTH_LONG).show();


                Intent comenzar = new Intent(MainActivity.this, Tabloyout.class);
                startActivity(comenzar);



            }

      })
               .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception exception) {
                        progressDialog.dismiss();
                        Toast.makeText(getApplicationContext(), exception.getMessage(), Toast.LENGTH_LONG).show();
                    }
                })
                .addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                        //displaying the upload progress
                        double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();

                        progressDialog.setMessage("Cargando " + ((int) progress) + "%...");
                    }
                });
    
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.upload,menu);
    return true;
}


@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {

    switch (item.getItemId()){

        case R.id.subir:

            uploadFile();

            break;


    }

    return super.onOptionsItemSelected(item);
}





 }

IGNORAR Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

1 respuesta 1

1

En la siguiente linea de tu código declaras el path que tendrá tu imagen: storageReference=FirebaseStorage.getInstance().getReference(Constantes.STORAGE_PATH_UPLOADS);

Le estas pasando una constante (Constantes.STORAGE_PATH_UPLOADS) como parámetro a getReference(), es por este motivo que todas las imágenes las sube a la misma ruta y se están sobreescribiendo. Prueba pasarle como parámetro a getReference() la fecha actual en formato string, esto hará que nunca existan dos imagenes con el mismo path

1
  • 1
    Gracias crack, lo resolvi con tu respuesta. saludos desde Republica Dominicana
    – Osdward
    el 28 oct. 2020 a las 14:56

Tu Respuesta

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

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.