尝试下载和保存视频时,Android Java.io.FileNotFoundException

时间:2016-05-05 16:16:45

标签: android exception download movies

我正在尝试开发一款提供很多电影的应用,你可以播放或下载电影。

该应用在我的 rooted Galaxy S4(Kitkat API:19)中运行良好, 但是当我在另一台设备上安装它并尝试下载电影时它会给我一个异常Java.io.FileNotFoundException,我不知道是什么问题

这是我的代码

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);

    }

}
顺便说一下 我把这个权限放在了制作中

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

任何帮助,请

1 个答案:

答案 0 :(得分:1)

正如我所说,你应该使用DownloadManager来获得你想要的东西。

以下是从给定网址下载文件的方法。

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);
}

像这样实例化DownloadManager

DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);

请务必添加WRITE_PERMISSIONINTERNET_PERMISSION,不要忘记在Android M或更新版本中,您应该以不同的方式检查权限。