0

Using this code i am able to download file from URL in Android and saving it into SDCard, is there any way to open this file programatically ?

Can Intent help in this ?

private static class Task extends AsyncTask<Void, Void, Void> {

    static String DownloadUrl = "http://00.00.00.00/abc.crt";
    static String fileName = "def.crt";

Async Task to download

@Override
protected Void doInBackground(Void... arg0) {
    DownloadFromUrl();
    return null;
}


public static void DownloadFromUrl() {

   try {
           File root = android.os.Environment.getExternalStorageDirectory();               

           File dir = new File (root.getAbsolutePath() + "/SDCard");
           if(dir.exists()==false) {
                dir.mkdirs();
           }

           URL url = new URL(DownloadUrl); //you can write here any link
           File file = new File(dir, fileName);

           long startTime = System.currentTimeMillis();
           Log.d("DownloadManager", "download begining");
           Log.d("DownloadManager", "download url:" + url);
           Log.d("DownloadManager", "downloaded file name:" + fileName);

           /* Open a connection to that URL. */
           URLConnection ucon = url.openConnection();

           /*
            * Define InputStreams to read from the URLConnection.
            */
           InputStream is = ucon.getInputStream();
           BufferedInputStream bis = new BufferedInputStream(is);

           /*
            * Read bytes to the Buffer until there is nothing more to read(-1).
            */
           ByteArrayBuffer baf = new ByteArrayBuffer(5000);
           int current = 0;
           while ((current = bis.read()) != -1) {
              baf.append((byte) current);
           }

           /* Convert the Bytes read to a String. */
           FileOutputStream fos = new FileOutputStream(file);
           fos.write(baf.toByteArray());
           fos.flush();
           fos.close();
           Log.d("DownloadManager", "download ready in" + ((System.currentTimeMillis() - startTime) / 1000) + " sec");




   } catch (IOException e) {
       Log.d("DownloadManager", "Error: " + e);
   }

}


}
tshepang
  • 12,111
  • 21
  • 91
  • 136
user1223035
  • 241
  • 2
  • 21
  • how do you want to open it? As text or binary or with an application? – Gjordis Feb 21 '13 at 09:04
  • Yes, `Intents` are the way to go; see Google or [this](http://stackoverflow.com/questions/7009452/how-to-launch-browser-to-open-local-file). – adrianp Feb 21 '13 at 09:04
  • When you say 'open', what do you mean? What's the filetype (as in what's .crt)? What's the data? String? Audio? – Matt Taylor Feb 21 '13 at 09:05

1 Answers1

0

Use a FileInputStream and load the downloaded file to memory http://developer.android.com/reference/java/io/FileInputStream.html

If the file is of a specific extension there might be other ways of loading it that doesn't involve resolving it to a byte array

Udrian
  • 76
  • 6
  • Thanks Udrian & Matt, Ideally .CRT is VPN Certificate file, which when installed popups up notification to enter Certificate Name and then Installs file for Android VPN app. I guess if we want to read a file then we require FileInputStream, to open this file should not intents be used ? – user1223035 Feb 21 '13 at 09:12
  • GJordis it will be opened as binary file which would invoke VPN App when opened – user1223035 Feb 21 '13 at 09:15
  • I somehow missed that it was a .CRT file. And yes, if your intention is to prompt user to open this file outside of your application then you should use a Intent. This might help you a bit http://indyvision.net/2010/03/android-using-intents-open-files/ – Udrian Feb 21 '13 at 09:16
  • 1
    Thanks Udrian, I tried the same thing, but as i am using Async Task it won't allow me to invoke intent there. `File videoFile2Play = new File("/sdcard/nice_movie.mpeg"); Intent i = new Intent(); i.setAction(android.content.Intent.ACTION_VIEW); i.setDataAndType(Uri.fromFile(videoFile2Play), "video/mpeg"); startActivity(i);` – user1223035 Feb 21 '13 at 09:18
  • Have you tried invoking it on the main thread? http://stackoverflow.com/questions/4388415/call-main-thread-from-secondry-thread-in-java-android – Udrian Feb 21 '13 at 09:20
  • Udrian can u share an example as a reference to code i post above !! – user1223035 Feb 21 '13 at 09:23
  • activity.runOnUiThread(new Runnable() { public void run() { File file = new File(fileDir); Intent i = new Intent(); i.setAction(android.content.Intent.ACTION_RUN); i.setDataAndType(Uri.fromFile(file), mimeType); startActivity(i); } }); I don't really know how to specify the intent in order to open a crt file. – Udrian Feb 21 '13 at 09:41
  • I guess this could be one of the way to do, but not sure where to post in above code, as Async Task will not accept intent if(file.exists()){ Log.d("DownloadManager", "file.exists"); Intent intent = new Intent(Intent.ACTION_VIEW, Uri.fromFile(file)); context.startActivity(intent); } – user1223035 Feb 21 '13 at 09:41