Download PDF on Android [closed]

0

I have a button and I want to make that when I press it, a .PDF that is saved on the server is downloaded to the phone.

How can I do it?

    
asked by Sergio 27.03.2017 в 11:15
source

1 answer

2

Here you go!

public class ActividadEjemplo extends Activity {

    Button button;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        button = (Button) findViewById(R.id.miboton);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DescargarPDF();
            }
        });
    }

    public void DescargarPDF() {
        try {

            URL url = new URL("tu sitio");
            HttpURLConnection c = (HttpURLConnection) url.openConnection();
            c.setRequestMethod("GET");
            c.setDoOutput(true);
            c.connect();

            String Path = Environment.getExternalStorageDirectory() + "/download/";
            Log.v("PdfManager", "PATH: " + Path);
            File file = new File(Path);
            file.mkdirs();
            FileOutputStream fos = new FileOutputStream("tusitio.pdf");

            InputStream is = c.getInputStream();

            byte[] buffer = new byte[702];
            int len1 = 0;
            while ((len1 = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len1);
            }
            fos.close();
            is.close();
        } catch (IOException e) {
            Log.d("PdfManager", "Error: " + e);
        }
        Log.v("PdfManager", "Check: ");
    }

}

I also leave you a link to a similar answer in StackOverFlow.

  

LINK QUESTION ANSWER

    
answered by 27.03.2017 / 13:10
source