How to Use URL by replacing File Storage path in Android

1.1k views Asked by At

I am Printing Pdf using Google Cloud Printing from my Storage of my phone. i want to use pdf URL by replacing this. How to use URL? For Example: i want to replace /print/test.pdf" to "www.example.com/print/test.pdf"

File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/print/test.pdf");
       Intent printIntent = new Intent(MainActivity.this,PrintDialogActivity.class);
       printIntent.setDataAndType(Uri.fromFile(file),"application/pdf");
       printIntent.putExtra("title", "Android print demo");
         startActivity(printIntent);
1

There are 1 answers

0
AudioBubble On BEST ANSWER

Download http://central.maven.org/maven2/commons-io/commons-io/2.4/commons-io-2.4.jar and paste it to your libs folder in Android Studio. Open your Gradle file and inside the dependencies add this "compile files('libs/commons-io-1.3.2.jar')"

                URL url = null;
                try {
                    url = new URL("http://www.cbu.edu.zm/downloads/pdf-sample.pdf");
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
                String tDir = System.getProperty("java.io.tmpdir");
                String path = tDir + "tmp" + ".pdf";
                File file = new File(path); file.deleteOnExit();

                try {
                    FileUtils.copyURLToFile(url, file);
                } catch (IOException e) {
                    e.printStackTrace();
                }

                Intent printIntent = new Intent(MainActivity.this,
                        PrintDialogActivity.class);
                printIntent.setDataAndType(Uri.fromFile(file),
                        "application/pdf");
                printIntent.putExtra("title", "Android print demo");
                startActivity(printIntent);