2017-08-10 93 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" /> 
相关问题