Error sending pdf to Firebasestorage

Asked

Viewed 67 times

1

I am trying to send PDF files to firebase Storage and with this code I can go to the point of picking up Uri, sending, but only falls into onFailure, follows 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)

In some results on the internet said it was problem with play services but nothing was solved.

  • FileNotFoundException File not found.

  • but I catch the Uri of it, to display in a tect view, send it as parameter(pdfUri is the url of the file location, commented wrong there), the code runs in the debug correctly until the . Childs, to almost solving yesterday, I saw that I had managed to send one, but as it fell in Failure dps n understood comes and I decided to keep changing.

  • I posted the complete mistakes, I am since Saturday in this problem, I can’t imagine the error

1 answer

1


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

So I don’t even have 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());
    }
}

Browser other questions tagged

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