Error sending pdf to FirebaseStorage

1

I'm trying to send pdf files to the Firebase Storage and with this code I get to the point of getting the Uri, send, but only falls on onFailure, follow the code

            @Override
        public void onClick(View v) {
            if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
                selectPDF();
            } else {
                ActivityCompat.requestPermissions((Activity) getContext(), new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 9);
            }
        }
    });

    botaoEnviarArquivo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (pdfUri != null) {
                uploadFile(pdfUri);
                textView.setText(pdfUri.toString());
            } else
                Toast.makeText(getContext(), "Selecione um arquivo", Toast.LENGTH_SHORT).show();
            textView.setText(pdfUri.toString());
        }
    });

    return view;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    //checka se o usuario selecionou um arquivo ou nao
    if (requestCode == 86 && resultCode == RESULT_OK && data != null) {
        pdfUri = data.getData();//retorna o uid do pdf
        textView.setText(pdfUri.toString());
    } else
        Toast.makeText(getContext(), "Por favor,selecione um arquivo", Toast.LENGTH_SHORT).show();
}

//Tratando negação das permissoes
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == 9 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        selectPDF();
    } else
        Toast.makeText(getContext(), "Por favor, permita o acesso", Toast.LENGTH_SHORT).show();
}

//mpetodo para fazer o upload
private void uploadFile(Uri pdfUri) {
    Uri file = Uri.fromFile(new File(pdfUri.toString()));
    try {

        String nome = Base64Custom.codificarBase64(nomeArquivo.getText().toString());
        StorageReference riversRef = storageReference
                .child("arquivos")
                .child(nome);
        UploadTask uploadTask = riversRef.putFile(file);
        uploadTask.addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(getContext(), "Falhou", Toast.LENGTH_SHORT).show();
            }
        }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                Toast.makeText(getContext(), "Enviado", Toast.LENGTH_SHORT).show();
            }
        });
    }catch (Exception e){
        Toast.makeText(getContext(),"Digite um nome para o arquivo",Toast.LENGTH_SHORT).show();
    }
}

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

The errors in logcat are as follows.

09-18 02:54:01.585 15429-15805/ktm.com.menu E/StorageException: /content:/com.google.android.apps.docs.storage/document/acc%3D1%3Bdoc%3D61189 (No such file or directory)
    java.io.FileNotFoundException: /content:/com.google.android.apps.docs.storage/document/acc%3D1%3Bdoc%3D61189 (No such file or directory)


     Code: -13000 HttpResult: 0
09-18 02:54:01.583 15429-15805/ktm.com.menu E/StorageException: /content:/com.google.android.apps.docs.storage/document/acc%3D1%3Bdoc%3D61189 (No such file or directory)
    java.io.FileNotFoundException: /content:/com.google.android.apps.docs.storage/document/acc%3D1%3Bdoc%3D61189 (No such file or directory)

Some results on the internet said it was a problem with play services but nothing was resolved.

    
asked by anonymous 18.09.2018 / 08:01

1 answer

1

I was able to fix the error, I was not loading the file in a certain way, so I had to use a ByteOutoutStream and make an array of bytes

So I do not even need to use Uri

 private void uploadFile() {
    try {
        //Uri file = Uri.fromFile(new File(pdfUri.getEncodedPath().toString()));
        String fileName = nomeArquivo.getText().toString();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] dadosFile = baos.toByteArray();

        StorageMetadata metadata = new StorageMetadata.Builder()
        .setContentType("application/pdf")
                .build();
        StorageReference riversRef = storageRef
                .child("arquivos").child(fileName);
        UploadTask uploadTask = riversRef.putBytes(dadosFile,metadata);
        uploadTask.addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                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) {
                Toast.makeText(getContext(), "Enviado", Toast.LENGTH_SHORT).show();
            }
        });
    }catch (Exception e){
        Toast.makeText(getContext(),"Digite um nome para o arquivo",Toast.LENGTH_SHORT).show();
        textView.setText(e.toString());
    }
}
    
18.09.2018 / 22:15