0

can you please tell me how to download file from server ? I am getting error fill not found error ?

07-30 17:10:28.849: W/System.err(14900): java.io.FileNotFoundException: /testnaveen: open failed: EROFS (Read-only file system)
07-30 17:10:28.849: W/System.err(14900):    at libcore.io.IoBridge.open(IoBridge.java:409)
07-30 17:10:28.849: W/System.err(14900):    at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
07-30 17:10:28.849: W/System.err(14900):    at java.io.FileOutputStream.<init>(FileOutputStream.java:128)
07-30 17:10:28.849: W/System.err(14900):    at java.io.FileOutputStream.<init>(FileOutputStream.java:117)
07-30 17:10:28.849: W/System.err(14900):    at com.mobilecem.atms.GlobalFunction.downloadFileFromServer(GlobalFunction.java:147)

I am doing like this

public static void downloadFileFromServer(String filename, String urlString) throws MalformedURLException, IOException
    {
        BufferedInputStream in = null;
        FileOutputStream fout = null;
        try
        {
            URL url = new URL(urlString);

            in = new BufferedInputStream(url.openStream());
            fout = new FileOutputStream(filename);

            byte data[] = new byte[1024];
            int count;
            while ((count = in.read(data, 0, 1024)) != -1)
            {
                fout.write(data, 0, count);
                System.out.println(count);
            }
        }
        finally
        {
            if (in != null)
                    in.close();
            if (fout != null)
                    fout.close();
        }
        System.out.println("Done");
    }

I call both ways but hot same error why ?

GlobalFunction.downloadFileFromServer("test", "http://www.example.com/inputParameters.js");
GlobalFunction.downloadFileFromServer( new File(Activity.this.getFilesDir(),"www").getAbsolutePath(), "url");
  • Make sure your file is not read-only. Have provided WRITE_EXTERNAL_STORAGE permission in your manifest? – GrIsHu Jul 30 '14 at 11:58
  • Already written in menifest file ..please download file from server –  Jul 30 '14 at 11:59
  • Make sure your file name is correct and also its uploaded on server. – GrIsHu Jul 30 '14 at 12:01
  • it is uploaded on server if you hit the like as given there is file .But what is file name ? i mention anything –  Jul 30 '14 at 12:02

2 Answers2

0

You need to use AsyncTask to invoke network related operation if you are using HONEYCOMB or higher.

See this answer

Community
  • 1
  • 1
BrainCrash
  • 12,992
  • 3
  • 32
  • 38
0

Use Download Manager. Advantage of Download Manager:

  1. It Automatically retry the download if connection lost.
  2. It Takes Care of Worker Thread i.e Background Thread .
  3. It Shows your downloaded status with progress status in Navigation Tray

    package com.example.testing;
    import android.app.Activity;
    import android.app.DownloadManager;
    import android.content.Context;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.Environment;
    import android.util.Log;
    
    public class FileDownloading extends Activity
    {
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startDownload("http://www.example.com/inputParameters.js","inputParameters.js");
    }
    public void startDownload(String url ,String fileName)
    {
        Log.d("Download Url", url);
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
        request.setTitle("Your File Name");
        request.setDescription(fileName+" is downloading..");
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
        DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); 
        long downloadID = downloadManager.enqueue(request); 
    }
     }
    

Don't forget to use Internet and Storage permission in manifest.xml

GrIsHu
  • 29,068
  • 10
  • 64
  • 102
VegeOSplash
  • 214
  • 1
  • 3