androidurlgoogle-cloud-printandroid-print-frameworkandroid-pdf-api

How to Use URL by replacing File Storage path in Android


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);

Solution

  • 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);