2017-08-16 182 views
0

喜的朋友有很多困惑。 第一件事情就是它不是在存儲設備上,而不是它存儲在我的模擬器這條路徑的數據/數據/ com.customfonts /文件/ Robotoo.ttf下。然後,當我試圖讓沒有找到,因爲它在數據/用戶/ 0/com.customfonts/Robotoo.ttf搜索,而不是搜索data.data/com.customfonts/files/Robotoo.ttf文件時拋出運行時異常文件。內部存儲不是存儲文件

getDirectory.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) 
     { 
      new DownloadingTask().execute(); 
      Log.i("FilePAthFirst",""+getFilesDir()); 
     } 
    }); 
    btnGETDATA.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 

      String filename="Robotoo.ttf"; 

      getTypeface(filename); 
     } 
    }); 

    private Typeface getTypeface(String filename) 
    { 

     Typeface font; 
     try 
     { 

      font = Typeface.createFromFile(getFilesDir() +"/"+filename); 
      Log.i("FOnt found",""+font); 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
      return null; 
     } 


    return font; 
} 

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(getApplicationContext().getFilesDir()+ "Robotoo.ttf"); 
      Log.i("Download","complete"); 
      Log.i("FOS",""+fos.toString()); 

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

回答

0

協調代碼以獲取保存文件和加載文件的文件路徑。

要加載TFF也使用getApplicationContext()。getFilesDir()+文件名

0

我用了你同樣的代碼,這是在回答它工作正常,可能是你可以檢查是否有權限對清單互聯網<uses-permission android:name="android.permission.INTERNET"></uses-permission>,如果你已經有了這個,你可以等到你AsyncTaskonPostExecute,並檢查您收到任何錯誤。

內部存儲

和內部存儲,它不會因爲data.data/com.customfonts/files/恆定在Android 6.0,這將是動態的,我們不應該硬編碼的路徑(你正在做的是正確的)

請參閱本Offical Doc

編輯:忘了早些時候加入寫在內部存儲文件按照Doc我們應該用openFileOutput所以我使用的。

private class DownloadingTask extends AsyncTask<Void,Void,Void> { 

    @Override 
    protected Void doInBackground(Void... voids) { 
     try { 
      URL url = new URL("[your url here]"); 
      HttpURLConnection c = (HttpURLConnection) url.openConnection(); 
      c.setRequestMethod("GET"); 
      c.connect(); 

      Log.d("sdsdfds", "doInBackground: " + getApplicationContext().getFilesDir()); 

      FileOutputStream fos = getApplicationContext().openFileOutput("Robotoo4.ttf", MODE_PRIVATE); 
      Log.i("Download","complete"); 
      Log.i("FOS",""+fos.toString()); 

      InputStream is = c.getInputStream(); 
      byte[] buffer = new byte[4 * 1024]; 
      int len1 = 0; 
      while ((len1 = is.read(buffer)) != -1) { 
       fos.write(buffer, 0, len1); 
      } 
      fos.close(); 
      is.close(); 
     } 
     catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return null; 
    } 

    @Override 
    protected void onPostExecute(Void aVoid) { 
     super.onPostExecute(aVoid); 

     File file = new File(getFilesDir() + "/" + "Robotoo4.ttf"); 
     Log.d("", "onPostExecute: " + file.exists() + " " + file.getAbsolutePath() + " Length " + file.length()); 
    } 
} 
+0

你要我分享什麼..? DownloadingTask ..? –

+0

@ Ssri89我更新了我的答案。 –