2012-12-06 68 views
4

這是我的代碼和下面的錯誤。在日誌中出現錯誤,表示無法創建外部文件目錄。無法創建外部文件目錄 - ANDROID

String downloadURL = getString(R.string.download_URL); 
Uri uri = Uri.parse(downloadURL); 
DownloadManager.Request request = new Request(uri); 
request.setAllowedNetworkTypes(Request.NETWORK_WIFI | Request.NETWORK_MOBILE); 
request.setAllowedOverRoaming(false); 
request.setTitle("Android Jelly Bean's Pic Download"); 
request.setDescription("Android Jelly Beans Pic Download using Download Manager"); 
request.setDestinationInExternalFilesDir(getApplicationContext(),  
Environment.DIRECTORY_DOWNLOADS, "abc.png"); 
downloadReference = dm.enqueue(request); 

具有錯誤的線是 request.setDestinationInExternalFilesDir(getApplicationContext(),
Environment.DIRECTORY_DOWNLOADS, 「abc.png」);

12-05 18:51:55.436: W/ApplicationContext(1049): Unable to create external files directory 
12-05 18:51:55.436: D/AndroidRuntime(1049): Shutting down VM 
12-05 18:51:55.446: W/dalvikvm(1049): threadid=1: thread exiting with uncaught exception (group=0x409961f8) 
12-05 18:51:55.476: E/AndroidRuntime(1049): FATAL EXCEPTION: main 
12-05 18:51:55.476: E/AndroidRuntime(1049): java.lang.NullPointerException: file 
12-05 18:51:55.476: E/AndroidRuntime(1049):  at android.net.Uri.fromFile(Uri.java:441) 
12-05 18:51:55.476: E/AndroidRuntime(1049):  at android.app.DownloadManager$Request.setDestinationFromBase(DownloadManager.java:504) 
12-05 18:51:55.476: E/AndroidRuntime(1049):  at android.app.DownloadManager$Request.setDestinationInExternalFilesDir(DownloadManager.java:466) 
12-05 18:51:55.476: E/AndroidRuntime(1049):  at com.example.downloadmanagerapplication.DownloadManagerActivity.startDownload(DownloadManagerActivity.java:93) 
12-05 18:51:55.476: E/AndroidRuntime(1049):  at com.example.downloadmanagerapplication.DownloadManagerActivity$1.onClick(DownloadManagerActivity.java:115) 
12-05 18:51:55.476: E/AndroidRuntime(1049):  at android.view.View.performClick(View.java:3480) 
12-05 18:51:55.476: E/AndroidRuntime(1049):  at android.view.View$PerformClick.run(View.java:13983) 
12-05 18:51:55.476: E/AndroidRuntime(1049):  at android.os.Handler.handleCallback(Handler.java:605) 
12-05 18:51:55.476: E/AndroidRuntime(1049):  at android.os.Handler.dispatchMessage(Handler.java:92) 
12-05 18:51:55.476: E/AndroidRuntime(1049):  at android.os.Looper.loop(Looper.java:137) 
12-05 18:51:55.476: E/AndroidRuntime(1049):  at android.app.ActivityThread.main(ActivityThread.java:4340) 
12-05 18:51:55.476: E/AndroidRuntime(1049):  at java.lang.reflect.Method.invokeNative(Native Method) 
12-05 18:51:55.476: E/AndroidRuntime(1049):  at java.lang.reflect.Method.invoke(Method.java:511) 
12-05 18:51:55.476: E/AndroidRuntime(1049):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
12-05 18:51:55.476: E/AndroidRuntime(1049):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
12-05 18:51:55.476: E/AndroidRuntime(1049):  at dalvik.system.NativeStart.main(Native Method) 
+1

我的一個機器人同事報告說,已知問題下載經理並建議我不要使用它。只是一個頭。 –

+0

@GrahamSmith請爲downloadmanger建議一些替代方法,目前我面臨的問題是無法寫入內部存儲器。 –

回答

11

爲了節省/將數據寫入文件或創建/刪除目錄,你必須設置「使用」權限在manifest文件。

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

樣品:Android DownloadManager

+0

抱歉忘了添加我有這些權限 <使用權限android:name =「android.permission.ACCESS_NETWORK_STATE」/> <使用權限android:name =「android.permission.ACCESS_WIFI_STATE」/> <使用權限android:name =「android.permission.INTERNET」/> <使用權限android:name =「android.permission.WRITE_EXTERNAL_STORAGE」/> –

+4

問題在於虛擬設備是在沒有SD卡內存分配的情況下創建的。 –

+1

如果目標設備沒有SD卡,該怎麼辦?該應用程序仍應該工作,對吧? –

0

你可以嘗試這樣的:

File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + fileName);

相反的:

request.setDestinationInExternalFilesDir(Context, Type, Path)

相關問題