2017-08-10 82 views
0

嗨朋友!, 我只是試圖從url下載圖像到內部存儲http://github.com/google/fonts/blob/master/apache/roboto/Roboto-Regular.ttf?raw=true。我創建了目錄。在Android下載url文件到內部存儲時出錯?

 try 
     { 
     File folder = new File(getFilesDir() + "/"+"SS"); 
     if (!folder.exists()) 
     { 
     folder.mkdir(); 
     Log.i("Directory","Created"); 
     } 
     //URL connection 
     URL url = new URL(fonturl); 
     HttpURLConnection c = (HttpURLConnection) 
     url.openConnection(); 
     c.connect(); 
     File apkStorage=new File(getFilesDir().getPath()); 
     Log.i("ApkStorage",""+apkStorage); 
     outputFile=new File(apkStorage.getAbsolutePath() +"/"); 
      if(!outputFile.exists()) 
      { 
      try { 
       outputFile.createNewFile(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      Log.e("FIle", "File Created"); 
     } 
     FileOutputStream fos = new FileOutputStream(outputFile); 
     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()); 
     } 
     } 

它只是創建文件名我指定download.jpg不從url下載文件到內部存儲。

錯誤:沒有在我的目錄下載字體顯示像這樣的錯誤下載錯誤異常/data/user/0/sdk.nfnlabs.in.customfonts/files(是一個目錄)。它應該直接下載到我的目錄,如roboto.tff而不提供文件名。

+0

是什麼錯誤可以用您的問題每股 –

+0

請分享錯誤日誌 – Gaurav

+0

請幫我在哪裏出錯了 – saiRam89

回答

0

你可以從HttpUrlConnection文件名類似下面

HttpURLConnection c = (HttpURLConnection) url.openConnection(); 
c.connect(); 
File apkStorage = new File(getFilesDir().getPath()); 
Log.i("ApkStorage",""+apkStorage); 
String raw = c.getHeaderField("Content-Disposition"); 
// raw = "attachment; filename=Roboto-Regular.ttf" 
String fileName; 
if(raw != null && raw.indexOf("=") != -1) { 
    fileName = raw.split("=")[1]; //getting value after '=' 
} else { 
    // fall back to random generated file name? 
    fileName = "unknown_file"; 
} 
outputFile = new File(apkStorage, fileName); 

希望能對大家有所幫助。

0

您可以使用下面的函數

public void downloadFile(Context context){ 

    public long enqueue; 
    public DownloadManager dm; 

    dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); 
    DownloadManager.Request request = new DownloadManager.Request(
      Uri.parse("http://github.com/google/fonts/blob/master/apache/roboto/Roboto-Regular.ttf?raw=true")); 
    File direct = new File(Environment.getExternalStorageDirectory() 
      + "/SS"); 

    if (!direct.exists()) { 
     direct.mkdirs(); 
    } 
    request.setAllowedNetworkTypes(
      DownloadManager.Request.NETWORK_WIFI 
        | DownloadManager.Request.NETWORK_MOBILE) 
      .setAllowedOverRoaming(false).setTitle("Downloading...") 
      .setDescription("Please wait. File is downloading...") 
      .setDestinationInExternalPublicDir("/SS","Roboto-Regular.ttf") 
      .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); 

    enqueue = dm.enqueue(request); 



    context.registerReceiver(receiver, new IntentFilter(
      DownloadManager.ACTION_DOWNLOAD_COMPLETE)); 
} 

廣播接收器類用於專有天氣下載完成與否。

public BroadcastReceiver receiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 

     String action = intent.getAction(); 
     if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) { 

      File direct = new File(Environment.getExternalStorageDirectory() 
        + "/SS"); 
      Toast.makeText(context,"File saved at location : "+direct.getAbsolutePath(),Toast.LENGTH_SHORT).show(); 

      context.unregisterReceiver(receiver); 
      Activity_Downloads.callDownloadWS(context); 

      long downloadId = intent.getLongExtra(
        DownloadManager.EXTRA_DOWNLOAD_ID, 0); 
      DownloadManager.Query query = new DownloadManager.Query(); 
      query.setFilterById(enqueue); 
      Cursor c = dm.query(query); 
      if (c.moveToFirst()) { 
       int columnIndex = c 
         .getColumnIndex(DownloadManager.COLUMN_STATUS); 
       if (DownloadManager.STATUS_SUCCESSFUL == c 
         .getInt(columnIndex)) { 

       } 
      } 
     } 
    } 
}; 

而且不要忘了添加

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
相關問題