0

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

Grender
  • 1,589
  • 2
  • 17
  • 44
M.Honi
  • 123
  • 1
  • 1
  • 12

1 Answers1

1

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.

Grender
  • 1,589
  • 2
  • 17
  • 44
  • unfortunately it still didnt work i create the method and i call it from the button Listener – M.Honi May 05 '16 at 17:17
  • Can you post your full logcat log? – Grender May 05 '16 at 17:18
  • please describe the argument uri=uri of what? path=path of what? filename: is the file i want to download or where i want to download on my device?? – M.Honi May 05 '16 at 17:32
  • The URL of the file you want to download, the path (folder under the SD card, ex: Pictures, Downloads... ) and the name of the file you want to store. – Grender May 05 '16 at 17:34
  • the code doesn't work, it doesn't give any error but it don't download the file too – M.Honi May 05 '16 at 18:56
  • It does work, I have tested it in my app. Can you post how are u calling it? – Grender May 05 '16 at 19:00
  • or see it here that's how i call it DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); downloadFileFromUrl(path, fileName, downloadManager); – M.Honi May 05 '16 at 19:20
  • i said that your answer is ok because it near from true, but still not work :) – M.Honi May 05 '16 at 19:43
  • I want to see the content of your `url, path and fileName` variables. – Grender May 05 '16 at 19:50
  • url="192.168.1.5:8080/BlueNet_NMCvideoShare/blue_elephant.mp4"; path=Environment.getExternalStorageDirectory() + File.separator + "BlueNet"; fileName=blue_elephant.mp4 – M.Honi May 05 '16 at 20:34
  • @M.Honi `path` must be only "/BlueNet" as `request.setDestinationInExternalPublicDir(path, fileName);` already sets the destination to the external SD card plus the dir you pass. (Maybe I've should called the variable `dir` instead of `path`, I'm gonna edit it. – Grender May 05 '16 at 23:41
  • ok, i wanna try this solution and if it doesn't work I'll wait you while you edit your answer – M.Honi May 06 '16 at 09:15
  • still not working, it doesn't give any error but not download any thing :( i put break point to see what happened exactly but I found nothing – M.Honi May 06 '16 at 09:24
  • I can't help you more without knowing more about your app's code, the problem is not in the download method. – Grender May 06 '16 at 15:21
  • so, what do you suggest? do you want me to send you the the whole main activity? if you so, give me your email cause i can't send it here – M.Honi May 06 '16 at 16:13
  • You can upload the important files to some website and post the link here, so I can check it out more effusively. – Grender May 06 '16 at 16:17
  • what file do you need? – M.Honi May 06 '16 at 16:24
  • Activities and/or fragments where you call the download method, manifest, the app gradle file and helper classes (if you are using them) – Grender May 06 '16 at 16:34
  • it's only main activity i'll post it here as an answer ok? – M.Honi May 06 '16 at 16:43
  • i can't edit my question because now i'm using donload manager – M.Honi May 06 '16 at 16:58
  • So, have you posted it anywhere? – Grender May 06 '16 at 22:48
  • i post it in new question here http://stackoverflow.com/questions/37082354/android-download-manager-not-working – M.Honi May 06 '16 at 22:53