File sent to firebase has 0Bytes

Asked

Viewed 36 times

0

I have a problem, I can send the pdf to the Storage, but it is coming there with 0Bytes. there is the code, and does not generate error, I believe I am not sending any file, but within this structure I would like to know how to get the PDF file.

    private void uploadFile() {
    try {
        final String nomeAutorArquivo = nomeAutor.getText().toString();
        final String fileName = nomeArquivo.getText().toString();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();//recupera o conteudo de entrada e converte em array de bytes
        final byte[] dadosFile = baos.toByteArray();//tira da memória e salva em um array de bytes

        final StorageMetadata metadata = new StorageMetadata.Builder()
                .setContentType("application/pdf")//especifica o tipo de arquivo/dados
                .build();

        final StorageReference riversRef = storageRef
                .child("arquivos").child(fileName);//nós referencia
        final UploadTask uploadTask = riversRef.putBytes(dadosFile,metadata);//envia arquivo
        uploadTask.addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {//em caso de falha
                Toast.makeText(getContext(), "Falhou", Toast.LENGTH_SHORT).show();
                Log.d("erro ",e.toString());
            }
        }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {//em caso de sucesso
                Toast.makeText(getContext(), "Enviado", Toast.LENGTH_SHORT).show();
                //Adicionar metodo de aumentar pontos do usuario

            }
        });
        Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
            @Override
            public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
                if (!task.isSuccessful()){
                    throw task.getException();
                }
                return riversRef.getDownloadUrl();
            }
        }).addOnCompleteListener(new OnCompleteListener<Uri>() {
            @Override
            public void onComplete(@NonNull Task<Uri> task) {
                if(task.isSuccessful()){
                    Uri downloadUrl = task.getResult();
                    Livro livro = new Livro(fileName,nomeAutorArquivo,downloadUrl.toString());
                    livro.salvar();
                }else Log.d("erro","a");
            }
        });
    }catch (Exception e){
        Toast.makeText(getContext(),"Digite um nome para o arquivo",Toast.LENGTH_SHORT).show();
        //textView.setText(e.toString());
    }
}
//metodo para selecionar o arquivo
private void selectPDF() {
    Intent intent = new Intent();
    intent.setType("application/pdf");
    intent.setAction(Intent.ACTION_GET_CONTENT);//pegando arquivos
    startActivityForResult(intent, 86);
}

1 answer

1

Solved In case anyone searches for this information.

//metodo para fazer o upload
private void uploadFile(){
    final String nomeAutorArquivo = nomeAutor.getText().toString();
    final String fileName = nomeArquivo.getText().toString();

    Toast.makeText(getContext(), "Enviado", Toast.LENGTH_SHORT).show();
    Uri file = Uri.fromFile(new File(pdfUri.toString()));
    final StorageReference riversRefTTT = storageRef
            .child("arquivos").child(fileName);//nós referencia
    UploadTask uploadTask1 = riversRefTTT.putFile(pdfUri);
    uploadTask1.addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Toast.makeText(getContext(),"Falha ao enviar!",Toast.LENGTH_SHORT).show();
        }
    }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

            Toast.makeText(getContext(),"Enviado!",Toast.LENGTH_SHORT).show();
        }
    });

    Task<Uri> urlTask = uploadTask1.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
        @Override
        public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
            if (!task.isSuccessful()){
                throw task.getException();
            }
            return riversRefTTT.getDownloadUrl();
        }
    }).addOnCompleteListener(new OnCompleteListener<Uri>() {
        @Override
        public void onComplete(@NonNull Task<Uri> task) {
            if(task.isSuccessful()){
                Uri downloadUrl = task.getResult();
                Livro livro = new Livro(fileName,nomeAutorArquivo,downloadUrl.toString());
                livro.salvar();
            }else Log.d("erro","a");
        }
    });
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.