2016-07-21 6 views
0

서버에서 파일을 다운로드하고 저장소에 저장하려고하지만 코드에 오류 - Unable to create directory이 표시됩니다. 오류를 확인하십시오안드로이드에서 디렉토리를 만들 수 없습니다. 오류

작업 - 파일이 서버에서 다운로드 된 다음 안드로이드의 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

로그를 게시하거나 h 그들을 ave. –

+0

'myFile.mkdir();'. 그것은'if (! myFile.mkdir()) return;'이어야하며 사용자에게 알려주는 축배를 표시합니다. – greenapps

+0

'코드가 오류를 발생시킵니다. ' 코드? 제발 정확 해. 어떤 진술 이요? 어느 선 이요? – greenapps

답변

1

안드로이드 매니페스트에 대한 사용 권한을 가지고 있는지 확인

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
또한

, 다음과 같이 외부 디렉토리를 참조 :

String name = Environment.getExternalStorageDirectory().getAbsolutePath() + "/DirectoryNameYouWant/" ; 
관련 문제