Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm downloading a file using volley and I'm not able to open it, it shows this:

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

Also mention that service returns an 11kb byte[] and debugging, I'm receiving an 16kb response. The code I'm using is:

InputStreamVolleyRequest request = new InputStreamVolleyRequest(Request.Method.POST, uri,
            new Response.Listener<byte[]>() {
                @Override
                public void onResponse(byte[] response) {                        
                    try {
                        if (response!=null) {

                            if(Build.VERSION.SDK_INT>22){
                                requestPermissions(new String[] {"android.permission.WRITE_EXTERNAL_STORAGE","android.permission.READ_EXTERNAL_STORAGE"}, 1);
                            }

                            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("\\","/"));
                            try {
                                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
                                bos.write(response);
                                bos.flush();
                                bos.close();
                                Log.d("NEWFILE", file.getAbsolutePath());
                                Toast.makeText(getActivity(), "Descarga completa.", Toast.LENGTH_LONG).show();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                        }
                    } catch (Exception e) {
                        Log.d("KEY_ERROR", "UNABLE TO DOWNLOAD FILE");
                        e.printStackTrace();
                    }
                }
            } ,new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    }, (HashMap<String, String>) params);

    RequestQueue mRequestQueue = Volley.newRequestQueue(getActivity().getApplicationContext(), new HurlStack());
    mRequestQueue.add(request);

Thanks in advance!

like image 728
Juan Avatar asked Nov 08 '22 06:11

Juan


1 Answers

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();
            }
        }
    }
like image 84
Juan Avatar answered Nov 11 '22 11:11

Juan