2016-07-21 8 views
0

サーバからファイルをダウンロードしてStorageに保存しようとしていますが、コードにエラー - Unable to create directoryがあります。エラーを確認してくださいAndroidでディレクトリを作成できません。エラー

タスク - ファイルがサーバーからダウンロードされてから、アンドロイドのwebviewに読み込まれます。

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    webView = (WebView) findViewById(R.id.webView); 
    try { 
     webView.loadUrl("file://" + Environment.getExternalStorageDirectory() + "/sponsors/"+ "dddd.html"); 
    } 
    catch (Exception e) 
    { 
     Toast.makeText(MainActivity.this, "File Doesn't Exist", Toast.LENGTH_SHORT).show(); 
    } 
    try { 
     myDownloadLast("http://192.168.76.1:8084/MyTest/dddd.html"); 
    } 
    catch (Exception e) 
    { 
     Toast.makeText(this, e.getMessage()+"\n"+e.getCause(), Toast.LENGTH_SHORT).show(); 
    } 
} 
public void myDownloadLast(String myURL) { 

    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(myURL)); 
    request.setTitle("Updating TimeTable"); 
    request.setDescription("Please Wait"); 

    //request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI); 
    request.allowScanningByMediaScanner(); 
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); 
    //request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN); 
    String nameOfFile = URLUtil.guessFileName(myURL, null, MimeTypeMap.getFileExtensionFromUrl(myURL)); 

    File myFile = new File(String.valueOf(Environment.getExternalStoragePublicDirectory("/sponsors/"))); 
    if(!myFile.exists()){ 
     myFile.mkdir(); 
    } 
    try { 
     request.setDestinationInExternalPublicDir(String.valueOf(myFile), nameOfFile); 
    } 
    catch (Exception e) 
    { 
     Toast.makeText(this, e.getMessage()+"\n"+e.getCause(), Toast.LENGTH_SHORT).show(); 
    } 
    DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); 
    manager.enqueue(request); 
    BroadcastReceiver onComplete = new BroadcastReceiver() { 
     public void onReceive(Context ctxt, Intent intent) { 
      //Toast.makeText(getActivity(), "Download Complete", Toast.LENGTH_LONG).show(); 
      Toast.makeText(getApplicationContext(), "Update Complete\nFor Best Performance\nRestart The App", Toast.LENGTH_SHORT).show(); 
     } 
    }; 
    registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); 
} 
あなたの問題は、いくつかの容疑者に関連することができ
+0

ログを投稿するか、それらをave。 –

+0

'myFile.mkdir();'これは 'if(!myFile.mkdir())return;'で、ユーザーに知らせるためにトーストを表示します。 – greenapps

+0

'しかし、コードはエラー'を返します。コード?どうぞよろしいですか?どの線? – greenapps

答えて

1

Androidのマニフェストに権限を持っていることを確認してください

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
また

、次のように外部ディレクトリを参照してください。

String name = Environment.getExternalStorageDirectory().getAbsolutePath() + "/DirectoryNameYouWant/" ; 
関連する問題