2017-08-15 121 views
1

我只是想從URL http://github.com/google/fonts/blob/master/apache/roboto/Roboto-Regular.ttf?raw=true文件在這裏下載到我的系統是我的代碼下載文件「java.io.FileNotFoundException:/用戶/文件(沒有這樣的文件或目錄)」

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

文件沒有下載,但拋出錯誤下載錯誤異常/用戶/文檔(沒有這樣的文件或目錄)

+1

'串根= Environment.getExternalStorageDirectory()的toString(); File myDir = new File(root +「/ saved_images」); '使用這個路徑而不是'/ Users/Documents'。 –

回答

2

存儲沒有「Users/Documents」目錄。讓我重寫你的代碼

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

此代碼將字體下載到您的應用程序的內部存儲。如果您想將其保存到外部存儲卡或得到一些信息,你可能會給一個鏡頭這篇文章:。 https://developer.android.com/training/basics/data-storage/files.html

+0

謝謝你,我怎麼能得到的文件,並插入到字體 – saiRam89

+0

只使用 '字樣的定製= Typeface.createFromFile(新文件(getFilesDir()「的Roboto-Regular.ttf」));' 請記住,如果你想在TextView中應用Typeface,你應該在UI中做到這一點線程 'runOnUiThread(new Runnable(){ public void run(){ textView.setTypeface(custom); } });'' – Fragile

相關問題