How to download files in Android using Java

343 Views Asked by At

I have an application which downloads an image from the NASA APOD. I have tried stream and DownloadManager but it just does not work.

Here is my current code.

private void download(String url) {
    DownloadManager manager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
        
    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
    request.setDestinationUri(Uri.fromFile(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)));
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    manager.enqueue(request);
}

I then call the method in a buttonClick:

normal.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {
        download("https://apod.nasa.gov/apod/image/0611/ngc6745_hst.jpg");
    }
});

This code doesn't show anything; no error and no notification. While the stream method I tried before gives me FileNotFoundException even though I created a new file using File.createNewFile(). Any help or suggestion are appreciated.

This is my Manifest

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        android:maxSdkVersion="28"/>`

My device runs in Android 11(API 30).

UPDATE

I change my download method above to:

private void download(String title, String url) throws IOException {
    File directory = getExternalFilesDir(Environment.DIRECTORY_DCIM);

    if (!directory.exists()) {
        directory.mkdir();
    }
    
    File fileName = new File(directory,title);
    
    if(!fileName.exists()){
        fileName.createNewFile();
    }

    URL u = new URL(url);

    HttpURLConnection connect = (HttpURLConnection) u.openConnection();

    if (connect.getResponseCode() != HttpURLConnection.HTTP_OK) {
        Log.w("ERROR SERVER RETURNED HTTP", connect.getResponseCode() + "");
    }

    try (InputStream is = connect.getInputStream();
     FileOutputStream fos = new FileOutputStream(fileName);) {
        byte[] bytes = new byte[1024];

        int b = 0;

        while ((b = is.read(bytes, 0, 1024)) != -1) {
            fos.write(bytes, 0, b);
        }
    }
}

The problem is the download is stored at /data/com.example.myapp/DCIM/.

1

There are 1 best solutions below

2
Thor On

I don't know why but download manager does not work for me it only create directory in the data/mypackage and never download the file.If any one can fix this please tell me so that I don't have to worry about ui notification.

For now this is my code that works:

private void download(String title, String url) throws IOException {
    File directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES+"/"+getPackageManager().getApplicationLabel(getApplicationInfo()));

    if (!directory.exists()) {
        directory.mkdir();
    }
    
    File fileName = new File(directory,title);
    
    if(!fileName.exists()){
        fileName.createNewFile();
    }

    URL u = new URL(url);

    HttpURLConnection connect = (HttpURLConnection) u.openConnection();

    if (connect.getResponseCode() != HttpURLConnection.HTTP_OK) {
        Log.w("ERROR SERVER RETURNED HTTP", connect.getResponseCode() + "");
    }

    try (InputStream is = connect.getInputStream();
     FileOutputStream fos = new FileOutputStream(fileName);) {
        byte[] bytes = new byte[1024];

        int b = 0;

        while ((b = is.read(bytes, 0, 1024)) != -1) {
            fos.write(bytes, 0, b);
        }
    }
}

That Environment.getExternalStoragePublicDirectory() is deprecated though.