N
N
newdancer2016-08-19 10:50:37
Android
newdancer, 2016-08-19 10:50:37

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());
    }
  }

I pass the path to the folder where I want to upload to the filePath file.
Application terminates with an error
java.lang.IllegalArgumentException: File app_/storage/emulated/0/Download contains a path separator

in line
directory = getActivity().getDir(filePath, Context.MODE_PRIVATE);

what is wrong in the code?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Smithson, 2016-08-19
@Smithson

app_

at the beginning of the path suggests that you are loading into the program directory (android has such a mod), most likely it is /storage/emulated/0/Android/data/your.application name
There are storage/emulated/0/ directories inside ? :)
If you want to write to the root of the flash drive, then you need a different context, most likely (I don’t remember exactly) MODE_WORLD_WRITEABLE

D
Dmitry, 2016-08-19
@Scotik

java.lang.IllegalArgumentException: File app_/storage/emulated/0/Download contains a path separator

um, as I understand it, the error is that the path to the directory must end with "/"
directory = getActivity().getDir(filePath + File.Separator, Context.MODE_PRIVATE);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question