Error When Downloading A File With Url Into Internal Storage Android Studio
I'm trying to download a mp3 music into internal storage, but conexion.connect(); seems like it have a error and end up going to the Exception. Here is my code: public class MainAc
Solution 1:
First of all, you need to add user permission in the manifest file :
<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE" />Next, ask the user to grant permission (you can put this code inside your onCreate) :
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_DENIED) {
Log.d("permission", "permission denied to WRITE_EXTERNAL_STORAGE - requesting it");
String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
requestPermissions(permissions, 1);
}
}
Use the following code :
try {
ProgressDialog progress;
UpdatedownloadAndInstall=newUpdate();
progress = newProgressDialog(YourActivity.this);
progress.setCancelable(false);
progress.setMessage("Downloading...");
downloadAndInstall.setContext(getApplicationContext(), progress);
downloadAndInstall.execute("http://yourURL.com/music.mp3");
} catch (Exception e) {
}
Now for the asynctask method :
publicclassUpdateextendsAsyncTask<String, Void, Void> {
private ProgressDialog progressDialog;
privateintstatus=0;
private Context context;
publicvoidsetContext(Context context, ProgressDialog progress) {
this.context = context;
this.progressDialog = progress;
}
publicvoidonPreExecute() {
progressDialog.setTitle("Downloading New Updates");
progressDialog.setMessage("This might take a while...");
progressDialog.setIndeterminate(false);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.show();
}
@Overrideprotected Void doInBackground(String... arg0) {
try {
URLurl=newURL(arg0[0]);
HttpURLConnectionc= (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
intfileLength= c.getContentLength();
ContextWrappercw=newContextWrapper(context);
StringPATH= Environment.getExternalStorageDirectory() + "/download/";
Filefile=newFile(PATH);
file.mkdirs();
FileoutputFile=newFile(file, "music.mp3");
FileOutputStreamfos=newFileOutputStream(outputFile);
InputStream is ;
intstatus= c.getResponseCode();
if (status != HttpURLConnection.HTTP_OK)
is = c.getErrorStream();
elseis= c.getInputStream();
byte[] buffer = newbyte[1024];
intlen1=0;
longtotal=0;
while ((len1 = is.read(buffer)) != -1) {
total += len1;
publishProgress((int) (total * 100 / fileLength));
fos.write(buffer, 0, len1);
}
fos.flush();
fos.close();
is.close();
} catch (FileNotFoundException fnfe) {
status = 1;
Log.e("File", "FileNotFoundException! " + fnfe);
} catch (Exception e) {
Log.e("UpdateAPP", "Exception " + e);
}
returnnull;
}
privatevoidpublishProgress(int i) {
progressDialog.setProgress(i);
}
publicvoidonPostExecute(Void unused) {
progressDialog.dismiss();
}
}
This method will download the file to your Download directory in internal storage. And also I added progress dialog to show the download progress.

Post a Comment for "Error When Downloading A File With Url Into Internal Storage Android Studio"