0

Here's the code to download a 3pg video file. Maybe one can give me an idea about what's wrong with it. I really need help. Can't get it to download the file:

Blockquote

        protected String doInBackground(String... sUrl) {
            byte[] data = new byte[1024];
            int count = 0;
            FileOutputStream fos = null;
            BufferedOutputStream output = null;
            try {
            URL url= new URL(sUrl[0]);
            URLConnection connection = url.openConnection();
            connection.connect();
            Log.v("URL"," " + connection.getURL());
            Log.v("File ", "length = " + connection.getContentLength());
            // download the file
            BufferedInputStream input = new BufferedInputStream(connection.getInputStream());
            File f = new File(Environment.getExternalStorageDirectory().getPath() + "/twokids.3gp");
            f.setWritable(true,true);
            try {
                fos = new FileOutputStream(f);
                } catch (FileNotFoundException fnfe) {
                    fnfe.getStackTrace();
                } catch (SecurityException se) {
                    se.getStackTrace();
                }

                output = new BufferedOutputStream(fos);                                                                                                                        

            while ((count = input.read(data)) != -1) {
                Log.v("Count="," " + count);
                output.write(data, 0, count);
            }
            output.flush();
            output.close();
            input.close();
            Log.v("Download","Finished");
            } catch (IndexOutOfBoundsException iobe) {
            iobe.getStackTrace();
            } catch (IOException ioe) {
            ioe.getStackTrace();
            } catch (NullPointerException npe) {
            npe.getStackTrace();
            }
            catch (Exception e) {
            e.getStackTrace();
            }

        return null;
    }

And strangely enough, sometimes the method connection.getContentLength() returns -1 instead of the file length.

Monica
  • 389
  • 1
  • 7
  • 19
  • And this code works on your device? Sorry for this question, but you eventually learn not to be sure in anything here :) – Marko Lazić May 24 '13 at 21:40
  • I only tried this code with the emulator. I don't have the real android device. – Monica May 24 '13 at 23:50
  • Did you set your permissions for using network? Check your URL whether is proper. If you have an error post a stacktrace. – Marko Lazić May 25 '13 at 01:27
  • Yes, I have the permission set for network in the manifest file. The URL is correct. (It starts downloading but stops after about downloading about 9K of the file.) I can download the file directly from that URL, but not with the android code. – Monica May 25 '13 at 04:16
  • I only get this error: 05-25 04:31:57.434: E/Trace(1445): error opening trace file: No such file or directory (2) 05-25 04:32:01.254: D/dalvikvm(1445): GC_CONCURRENT freed 137K, 10% free 2681K/2952K, paused 4ms+140ms, total 422ms 05-25 04:32:01.254: D/dalvikvm(1445): WAIT_FOR_CONCURRENT_GC blocked 362ms 05-25 04:32:01.464: D/dalvikvm(1445): GC_FOR_ALLOC freed 260K, 17% free 2725K/3264K, paused 117ms, total 163ms 05-25 04:32:01.773: D/dalvikvm(1445): GC_FOR_ALLOC freed 171K, 15% free 2858K/3332K, paused 137ms, total 188ms It refers to an existing file. Sorry for formatting – Monica May 25 '13 at 04:36
  • I tried the code on a real Android device, and it worked. I guess the emulators are not reliable. – Monica May 29 '13 at 23:39

0 Answers0