First, you must add permissions for AndroidManifest.xml:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> <uses-permission android:name="android.permission.INTERNET"></uses-permission> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
Secondly, add this code so that if the folder does not exist, it checks and creates the folder:
File root = android.os.Environment.getExternalStorageDirectory(); File dir = new File (root.getAbsolutePath() + "/download"); if(!dir.exists()) { dir.mkdirs(); }
Third, as Commons Ware reports, loading into the main thread is not good practice; You must implement it on a new thread; because at boot time the main thread is busy loading the file. If the wait time is longer than expected, the system will generate a Do Not Respond error.
For this you can use TaskAsync "or" IntentService "or even creating a new thread.
As you said, you are new to java, I suggest using " TaskAsync ", as it is very simple and easy.
source share