Answer the question
In order to leave comments, you need to log in
How to download an mp3 file via a direct link to your phone?
How to download an mp3 file via a direct link to your phone?
in I pass to AsyncTask a direct link to download the mp3 file new GetSoundTask().execute(linkPath);
AsyncTask itself looks like this
private class GetSoundTask extends AsyncTask<String, Integer, String>
{
protected void onPreExecute()
{
progressDownload.setProgress(0);
}
protected String doInBackground(String... urls)
{
String filename = "android.mp3";
directory = getActivity().getDir(filePath, Context.MODE_PRIVATE);
try {
File myFile = new File(directory, filename);
URL url = new URL(urls[0]);
URLConnection connection = url.openConnection();
connection.connect();
int fileSize = connection.getContentLength();
InputStream is = new BufferedInputStream(url.openStream());
OutputStream os = new FileOutputStream(myFile);
byte data[] = new byte[1024];
long total = 0;
int count;
while ((count = is.read(data)) != -1)
{
total += count;
publishProgress((int) (total * 100 / fileSize));
os.write(data, 0, count);
}
os.flush();
os.close();
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return filename;
}
protected void onProgressUpdate(Integer... progress)
{
textProgress.setText(String.valueOf(progress[0]) + "%");
progressDownload.setProgress(progress[0]);
}
protected void onCancelled()
{
Toast.makeText(context, "Error connecting to Server", Toast.LENGTH_LONG).show();
}
protected void onPostExecute(String filename)
{
progressDownload.setProgress(100);
mInfoTextView.setText("Загрузка завершена...");
File myFile = new File(directory, filename);
onDismiss(getDialog());
}
}
java.lang.IllegalArgumentException: File app_/storage/emulated/0/Download contains a path separator
directory = getActivity().getDir(filePath, Context.MODE_PRIVATE);
Answer the question
In order to leave comments, you need to log in
app_
java.lang.IllegalArgumentException: File app_/storage/emulated/0/Download contains a path separator
directory = getActivity().getDir(filePath + File.Separator, Context.MODE_PRIVATE);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question