2017-08-16 165 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我更新了我的答案。 –