如何使用Java在Android中下载文件

问题描述 投票:0回答:1

我有一个从 NASA APOD 下载图像的应用程序。我已经尝试过 Stream 和 DownloadManager 但它不起作用。

这是我当前的代码。

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

然后我在buttonClick中调用该方法:

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

这段代码没有显示任何内容;没有错误,也没有通知。虽然我之前尝试过的流方法给了我 FileNotFoundException,即使我使用 File.createNewFile() 创建了一个新文件。如有任何帮助或建议,我们将不胜感激。

这是我的清单

<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"/>`

我的设备运行 Android 11(API 30)。

更新

我将上面的下载方法更改为:

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

问题是下载存储在/data/com.example.myapp/DCIM/。

java android android-download-manager
1个回答
0
投票

我不知道为什么,但下载管理器对我不起作用,它只在 data/mypackage 中创建目录,而从不下载文件。如果有人可以解决此问题,请告诉我,这样我就不必担心 ui通知。

目前这是我有效的代码:

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

Environment.getExternalStoragePublicDirectory() 已被弃用。

© www.soinside.com 2019 - 2024. All rights reserved.