1

I have a download button in my app.When I press the download button it downloads an audio file from provided url.But the problem is I want to save the file in my internal storage folder called "DownloadTestFolder",it is showing an error.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Download"
        android:onClick="Download"
        android:id="@+id/download"/>

</LinearLayout>
public class DownloadTest extends Activity {

    DownloadManager downloadManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.download_test);
    }

    public void Download(View view){

        downloadManager=(DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE);
        Uri uri=Uri.parse("My Url");
        DownloadManager.Request request = new DownloadManager.Request(uri);
        try{
            request.setDestinationInExternalPublicDir(getFilesDir()+"/DownloadTestFolder","/");
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            Long reference = downloadManager.enqueue(request);

        }catch (Exception e){
            /* Error
        }

    }
}
rahul
  • 3
  • 1
  • 1
Furqan Hussain
  • 135
  • 1
  • 4
  • 14

1 Answers1

3

The DownloadManager can only download to external storage, you need to implement your own download manager.

Use this method to save file:

// DownloadFile AsyncTask
    private class DownloadFile extends AsyncTask<String, Void, Bitmap> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Bitmap doInBackground(String... URL) {

            String imageURL = URL[0];

            Bitmap bitmap = null;
            try {
                // Download Image from URL
                InputStream input = new java.net.URL(URL).openStream();
                // Decode Bitmap
                bitmap = BitmapFactory.decodeStream(input);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return bitmap;
        }

        @Override
        protected void onPostExecute(Bitmap result) {

            if (result != null) {
                File dir = new File(mContext.getFilesDir(), "MyImages");
                if(!dir.exists()){
                    dir.mkdir();
                }
                File destination = new File(dir, "image.jpg");

                try {
                    destination.createNewFile();
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    result.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
                    byte[] bitmapdata = bos.toByteArray();

                    FileOutputStream fos = new FileOutputStream(destination);
                    fos.write(bitmapdata);
                    fos.flush();
                    fos.close();
                    selectedFile = destination;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

Usage:

new DownloadFile().execute("url_here");

Note: I have added code for image type file

jox
  • 2,218
  • 22
  • 32
Quick learner
  • 10,632
  • 4
  • 45
  • 55