android cannot display PDF (name.pdf is of invalid format)

android cannot display PDF (name.pdf is of invalid format)

Finally I send the response as string and then decode it with android.util.Base64 . This is the code:

StringRequest sr = new StringRequest(Request.Method.POST, uri, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try{
                File baseDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
                baseDir.mkdirs();
                File carpeta = new File(baseDir + nombre_carpeta);
                carpeta.mkdirs();
                File file = new File(baseDir, documento.NombreFichero.replace(\,/));
                BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(file));

                FileOutputStream fos = new FileOutputStream(file);

                byte[] respuesta = android.util.Base64.decode(response.getBytes(), android.util.Base64.DEFAULT);

                fos.write(respuesta);

                fos.flush();

                fos.close();
                output.close();
            }catch(IOException e){
                e.printStackTrace();
            }
        }
    }

This is not a programming problem. I suggest that in your emulator or device, you install a pdf reader (Google PDF Viewer).

If you are using an emulator, it may be necessary for you to use a version that has Google Play already installed. And then you can install the pdf reader.

android cannot display PDF (name.pdf is of invalid format)

Leave a Reply

Your email address will not be published.