2

i follow more explain in this site for download mp3 or picture from URL , I follow more method and try to write my method but when i run application it stop.

I make method to query download when click
also put permission for INTERNET & WRITE_EXTERNAL_STORAGE

put the problem is still

this method is download

public static void downloadMain(){
    File fileToSave = null;
    String scrPath ="http://***";
    BufferedInputStream bis;
    BufferedOutputStream bos;

    fileToSave = new File(Environment.getExternalStorageDirectory().getPath() +"/"+ "A"+"/");
    if (!fileToSave.exists())
        fileToSave.mkdirs();
    fileToSave = new File(Environment.getExternalStorageDirectory().getPath() +"/"+ "A" +"/" + "h"+"/");
    if (!fileToSave.exists())
        fileToSave.mkdirs();
    File file = new File (fileToSave,"***.mp3");
    try{
        URL url = new URL(scrPath+"***.mp3");
        URLConnection ucon = url.openConnection();
        ucon.connect();
        bis=new BufferedInputStream(ucon.getInputStream());

        bos = new BufferedOutputStream(new FileOutputStream(file));
        bis=new BufferedInputStream(url.openStream());
        byte[] data = new byte[1024];
        int a =0;
        while(true){
            int k = bis.read(data);
            if(k==-1){
                bis.close();
                bos.flush();
                bos.close();

                break;
            }
            bos.write(data, 0, k);
            a+=k;
        }
    }catch(IOException e){}
}
Konrad Krakowiak
  • 12,285
  • 11
  • 58
  • 45
Abdu Hawi
  • 79
  • 2
  • 14

1 Answers1

0

I have three main perplexity about your program:

  1. Do you run the following code in an asynctask? (this must run asincronusly otherwise it will block)
  2. Why it loop infinitly?
  3. You couldn't open an url or a file named with a '*' inside of it

Edit:

You must run the download method asincronusly otherwise it wouldn't work, interaction with filesystem and network couldn't be done in the main thread

Edit2:

AsyncTask should be something like this

private class DownloadTask extends AsyncTask<String, Integer, String> {

 private Context context;

    public DownloadTask(Context context) {
        this.context = context;
    }

    @Override
    protected String doInBackground(String... sUrl) {
        InputStream input = null;
        OutputStream output = null;
        HttpURLConnection connection = null;
        try {
            URL url = new URL(sUrl[0]);
            connection = (HttpURLConnection) url.openConnection();
            connection.connect();

            // expect HTTP 200 OK, so we don't mistakenly save error report
            // instead of the file
            if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
                return "Server returned HTTP " + connection.getResponseCode()
                        + " " + connection.getResponseMessage();
            }

            // download the file
            input = connection.getInputStream();
            output = new FileOutputStream("/sdcard/file_name.extension");//put here your path and your mkdirs

            byte data[] = new byte[4096];
            long total = 0;
            int count;
            while ((count = input.read(data)) != -1) {

                total += count;
                output.write(data, 0, count);
            }
        } catch (Exception e) {
            return e.toString();
        } finally {
            try {
                if (output != null)
                    output.close();
                if (input != null)
                    input.close();
            } catch (IOException ignored) {
            }

            if (connection != null)
                connection.disconnect();
        }
        return null;
    }
 @Override
    protected void onPostExecute(String result) {
        if (result != null)
            Toast.makeText(context,"Download error: "+result, Toast.LENGTH_LONG).show();
        else
            Toast.makeText(context,"File downloaded", Toast.LENGTH_SHORT).show();
    }
}

And you shoould call it like this

DownloadTask downloadTask = new DownloadTask(YourActivity.this);
downloadTask.execute("the url to the file you want to download");

You could also have a look at this answer

Community
  • 1
  • 1
Matteo Corti
  • 484
  • 2
  • 13
  • 1- I try with asyncTask with three genric but it's not run also .2-the loop is read from inputstream until equal -1 after that stop. 3- i used correct url but hear I put * – Abdu Hawi Jan 13 '15 at 19:44
  • @user4450367 Could you put the code of your asynctask? – Matteo Corti Jan 13 '15 at 19:48
  • i try with this synctask but change the storge path and url also i query with onClickListener by null consturctor http://stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog – Abdu Hawi Jan 13 '15 at 20:13
  • Try with the one in my answer Your download main method have to contain only the call to the asynctask – Matteo Corti Jan 13 '15 at 20:14
  • i call the class in main class but i put the prameter for constructor null , i will try with no prametere – Abdu Hawi Jan 13 '15 at 20:22
  • No when you call the constructor you have to pass your class activity – Matteo Corti Jan 13 '15 at 20:27
  • also I put my main class constructor but is not working and not stop program . I want ask you : Am I need call DownloadManager class in my main class ? – Abdu Hawi Jan 13 '15 at 20:37
  • You need to call it in your btn onclick as context Young can pass getApplication() or getActivity() or your activity context. – Matteo Corti Jan 13 '15 at 21:09