我正在学习做Android应用程序。我想提出一个简单的申请。 但是我有一个问题,我不明白为什么。下载管理器在某些Android版本上不起作用。当我在4.1.1版本中按下下载按钮时,应用程序停止运行。下载管理器在什么版本上工作?
我的代码
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription(dlFilename);
request.setTitle(dlFilename);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir(downloadFolder, dlFilename);
DownloadManager manager = (DownloadManager) getSystemService(MainActivity.this.DOWNLOAD_SERVICE);
manager.enqueue(request);
Toast.makeText(getApplicationContext(), MainActivity.this.getString(R.string.downloading), Toast.LENGTH_LONG).show();
清单文件
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
LogCat
12-05 20:42:49.275 1510-1510/com.app.app/AndroidRuntime: FATAL EXCEPTION: main
java.lang.IllegalStateException: Unable to create directory: /storage/emulated/0/media/ringtones
at android.app.DownloadManager$Request.setDestinationInExternalPublicDir(DownloadManager.java:503)
at com.app.MainActivity$1$4$2.onClick(MainActivity.java:409)
at android.view.View.performClick(View.java:4204)
at android.view.View$PerformClick.run(View.java:17355)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
我正在等待您的帮助...谢谢。
答案 0 :(得分:0)
DownloadManager仅处理以http://或https://
开头的请求