0

I am just trying to download file from url http://github.com/google/fonts/blob/master/apache/roboto/Roboto-Regular.ttf?raw=true to my system here is my code

    getDirectory=(Button)findViewById(R.id.btn_newDirectory);
    getDirectory.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view)
        {
            new DownloadingTask().execute();
        }
    });
    private class DownloadingTask  extends AsyncTask<Void,Void,Void>{
    @Override
    protected Void doInBackground(Void... voids) {
        try {
            URL url = new URL(fonturl);
            HttpURLConnection c = (HttpURLConnection)     
            url.openConnection();
            c.setRequestMethod("GET");
            c.connect();
            FileOutputStream fos = new FileOutputStream("/Users/Documents");
            Log.i("Download","complete");
            InputStream is = c.getInputStream();
            byte[] buffer = new byte[1024];
            int len1 = 0;
            while ((len1 = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len1);
            }
            fos.close();
            is.close();
        }
            catch (Exception e) {
            e.printStackTrace();
            outputFile = null;
            Log.e("Error", "Download Error Exception " + e.getMessage());
        }

        return null;
    }
}

File is not downloaded but throwing error Download Error Exception /Users/Documents (No such file or directory)

saiRam89
  • 365
  • 1
  • 8
  • 21
  • 1
    `String root = Environment.getExternalStorageDirectory().toString(); File myDir = new File(root + "/saved_images"); ` use this path instead of `/Users/Documents`. – Arjun Gurung Aug 15 '17 at 10:23

1 Answers1

2

There is no "Users/Documents" directory on your storage. Let me rewrite your code a bit

getDirectory=(Button)findViewById(R.id.btn_newDirectory);
getDirectory.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view)
    {
        new DownloadingTask().execute();
    }
});
private class DownloadingTask  extends AsyncTask<Void,Void,Void>{
@Override
protected Void doInBackground(Void... voids) {
    try {
        URL url = new URL(fonturl);
        HttpURLConnection c = (HttpURLConnection)     
        url.openConnection();
        c.setRequestMethod("GET");
        c.connect();
        FileOutputStream fos = new FileOutputStream(new File(getFilesDir(),"Roboto-Regular.ttf")); // File you want to save to, It creates Roboto-Regular.ttf in your Internal Storage you got from "getFilesDir() method
        Log.i("Download","complete");
        InputStream is = c.getInputStream();
        byte[] buffer = new byte[1024];
        int len1 = 0;
        while ((len1 = is.read(buffer)) != -1) {
            fos.write(buffer, 0, len1);
        }
        fos.close();
        is.close();
    }
        catch (Exception e) {
        e.printStackTrace();
        outputFile = null;
        Log.e("Error", "Download Error Exception " + e.getMessage());
    }

    return null;
}}

This code downloads the font into internal storage of your app. If you'd like to save it into external storage or get some more information, you might give a shot to this article: https://developer.android.com/training/basics/data-storage/files.html

Fragile
  • 51
  • 5
  • Thank you how can i get file and insert into typeface – saiRam89 Aug 15 '17 at 11:05
  • just use `Typeface custom = Typeface.createFromFile(new File(getFilesDir(),"Roboto-Regular.ttf"));` Keep in mind that If you want to apply Typeface to TextView you should do it in UI Thread `runOnUiThread(new Runnable() { public void run() { textView.setTypeface(custom); } });` – Fragile Aug 15 '17 at 11:11