androidexceptiondownloadmovies

Android Java.io.FileNotFoundException when trying to download and save a video


I'm trying to develop an app which provides many movies and you can play it or download the movie.

That app works fine in my rooted Galaxy S4 (Kitkat API:19), but when I install it on another device and try to download the movie it gives my the exception Java.io.FileNotFoundException, I don't know what's the problem

That's my code

package nmc.net.blue.bluenet_nmc;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;
import java.net.URLConnection;

/**
 * Created by ElHoni on 5/3/2016.
 */
public class DownloadFileAsync extends AsyncTask<String, String, String> {



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

    @Override
    protected String doInBackground(String... aurl) {
        int count;

        try {
            URL url = new URL(aurl[0]);
            URLConnection conexion = url.openConnection();
            conexion.connect();

            File folder = new File(Environment.getExternalStorageDirectory() +
                    File.separator + "BlueNet");

            if (!folder.exists()) {
                folder.mkdirs();
            }


            String outputPath=folder.getPath()+"/"+aurl[1];

            BufferedInputStream input = new BufferedInputStream(url.openStream());
            FileOutputStream output = new FileOutputStream(outputPath);

            byte data[] = new byte[1024];

            long total = 0;

            while ((count = input.read(data, 0, 1024)) != -1) {
                output.write(data, 0, count);

            }

            output.flush();
            output.close();
            input.close();
        } catch (Exception e) {
            String err = e.toString();

        }
        return null;

    }
    protected void onProgressUpdate(String... progress) {
        Log.d("ANDRO_ASYNC",progress[0]);
    }

    @Override
    protected void onPostExecute(String unused) {

        //Toast.makeText(full_screen_video.this, "", 5);

    }

}

and by the way i put this permissions in manufests

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

any help please


Solution

  • As I said, you should use DownloadManager for what you want.

    Here is a method which downloads a file from a given URL.

    public static void downloadFileFromUrl(String url, String dir, String fileName, DownloadManager downloadManager) {
        Uri downloadUri = Uri.parse(url);
        DownloadManager.Request request = new DownloadManager.Request(downloadUri);
    
        request.setDestinationInExternalPublicDir(dir, fileName);
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        request.setVisibleInDownloadsUi(true);
        downloadManager.enqueue(request);
    }
    

    Instantiate DownloadManager like this:

    DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
    

    Remember to add WRITE_PERMISSION and INTERNET_PERMISSION and don't forget that in Android M or newer you should check the permissions in a different way.