簡體   English   中英

在設備上找不到下載的文件

[英]Can't find downloaded file on device

我正在編寫一個應用程序,單擊按鈕即可從URL下載文件。 問題是(顯然)成功下載后,我無法在SD卡中找到該文件。 我什至嘗試輸出Context.fileList()字符串數組,但其中不包含任何內容(導致錯誤日志“未創建文件”)。

我怎么能說完全執行了下載? 好吧,我看到一旦按下按鈕,數據連接就會立即激活,並且只有在3-4秒后才會放松,在此期間,我認為它正在下載小於100KB的文件。

這是主要活動的代碼:

package com.filedownloaddemo;

import java.io.File;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

    Button btn;
    // URL to download from
    String url = "http://www.edco.ie/_fileupload/The%20Interlopers%20-%20A%20short%20story%20by%20Saki.pdf";
    // file variable
    File outputFile;

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn = (Button) findViewById(R.id.button1);

        // set Android policy
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }

    // onClick handler for the button
    public void download(View v) {
        try {
            outputFile = new File(Environment.getExternalStorageDirectory() + File.separator + "myfile.pdf");
            DownloadHelper.downloadFile(url, outputFile);
        } catch (Exception e) {
            Log.d("DL_Error", e.getMessage());
        }

        if (this.fileList().length == 0) {
            Log.d("DL_Error", "No files created.");
        } else {
            // write file names to Log
            for (String s : this.fileList()) {
                Log.d("Download", s);
            }
        }
    }
}

這是下載的文件(摘自該社區的答案之一):

package com.filedownloaddemo;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

import android.util.Log;

public class DownloadHelper {

    public static void downloadFile(String url, File outputFile) {
        try {
            URL u = new URL(url);
            URLConnection conn = u.openConnection();
            int contentLength = conn.getContentLength();

            DataInputStream stream = new DataInputStream(u.openStream());

            byte[] buffer = new byte[contentLength];
            stream.readFully(buffer);
            stream.close();

            DataOutputStream fos = new DataOutputStream(new FileOutputStream(
                    outputFile));
            fos.write(buffer);
            fos.flush();
            fos.close();
        } catch (Exception e) {
            Log.d("DL_Error", e.getMessage()); 
        } 
    }
}

請幫忙!

一些事情:

Environment.getExternalStorageDirectory(); 

不是保存文件的正確位置,您需要給它一個文件名。

Environment.getExternalStorageDirectory() + "/myfile.pdf";

還要確保您在manifest.xml具有以下權限:

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

另外,不能保證onDestroy()會被調用,因此這不是檢查的好地方。

最后,要在將設備連接到PC時查看文件,可能需要讓MediaScanner知道有一個新文件要索引。

保存文件后發送廣播,以確保在MediaStore中將其拾取

Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file));
sendBroadcast(intent);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM