【问题标题】:Android Java.io.FileNotFoundException when trying to download and save a video尝试下载和保存视频时出现 Android Java.io.FileNotFoundException
【发布时间】:2016-08-31 12:36:59
【问题描述】:

我正在尝试开发一个提供许多电影的应用程序,您可以播放或下载电影。

该应用在我的 root 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" />

请帮忙

【问题讨论】:

  • 你应该使用DownloadManager而不是你的方法。

标签: android exception download movies


【解决方案1】:

正如我所说,您应该使用DownloadManager 来满足您的需求。

这是一种从给定 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);
}

像这样实例化DownloadManager

DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);

记得添加WRITE_PERMISSIONINTERNET_PERMISSION 并且不要忘记在Android M 或更高版本中您应该以不同的方式检查权限。

【讨论】:

  • 不幸的是它仍然没有工作我创建了方法,我从按钮监听器调用它
  • 您能发布完整的 logcat 日志吗?
  • 请描述 uri=uri 的参数是什么?路径=什么的路径?文件名:是我要下载的文件还是我要在设备上下载的位置??
  • 要下载的文件的URL、路径(SD卡下的文件夹,例如:Pictures、Downloads...)和要存储的文件的名称。
  • 代码不起作用,它没有给出任何错误但它也没有下载文件
猜你喜欢
  • 2017-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-05
  • 2021-09-03
  • 2021-01-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多