Ich versuche, Datei vom Server herunterzuladen und in Storage zu speichern, aber der Code gibt Fehler - Unable to create directory
. Bitte überprüfen Sie auf FehlerVerzeichnis konnte nicht in Android Error erstellt werden
Task - Datei wird vom Server heruntergeladen und dann ist es in Webview in Android geladen.
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));
}
Bitte, buchen Sie Ihre Protokoll oder Stack-Trace, wenn Sie h ave sie. –
'myFile.mkdir();'. Das sollte "if (! MyFile.mkdir()) return;" sein und einen Toast anzeigen, um den Benutzer zu informieren. – greenapps
'aber der Code gibt Fehler'. Der Code? Bitte seien Sie genau. Welche Aussage? Welche Linie? – greenapps