Android - 获取异步下载的原始文件名

4

我想在下载PDF文件时从网站中获取原始文件名,而不是将其重命名,并将其保存为相同的名称SD/Android/Data/(MyAPP)/Files目录下。目前,我必须告诉Android在查找文件时应该使用什么文件名以及在下载时应该将其保存为什么名称。是否有一种简单的方法可以更改此设置以将其保存为原始名称?谢谢。

private void startDownload() {

    String isFileThere = Environment.getExternalStorageDirectory()
            + "/Android/Data/" + getApplicationContext().getPackageName()
            + "/files/xxxxxxxx.pdf";
    File f = new File(isFileThere);

    if (f.exists()) {
        mProgressDialog.setProgress(0);
        showPdf();
    } else {

        DownloadFile downloadFile = new DownloadFile();
        downloadFile
                .execute(url);

    }
}

class DownloadFile extends AsyncTask<String, Integer, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mProgressDialog.show();
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {
        super.onProgressUpdate(progress);
        mProgressDialog.setProgress(progress[0]);
    }

    @Override
    protected String doInBackground(String... aurl) {

        try {

            URL url = new URL(aurl[0]);
            URLConnection connection = url.openConnection();

            connection.connect();
            int fileLength = connection.getContentLength();
            int tickSize = 2 * fileLength / 100;
            int nextProgress = tickSize;

            Log.d(

            "ANDRO_ASYNC", "Lenght of file: " + fileLength);

            InputStream input = new BufferedInputStream(url.openStream());

            String path = Environment.getExternalStorageDirectory()
                    + "/Android/Data/"
                    + getApplicationContext().getPackageName() + "/files";
            File file = new File(path);
            file.mkdirs();
            File outputFile = new File(file, "xxxxxxxx.pdf");

            OutputStream output = new FileOutputStream(outputFile);

            byte data[] = new byte[1024 * 1024];
            long total = 0;
            int count;
            while ((count = input.read(data)) != -1) {
                total += count;
                if (total >= nextProgress) {
                    nextProgress = (int) ((total / tickSize + 1) * tickSize);
                    this.publishProgress((int) (total * 100 / fileLength));
                }
                output.write(data, 0, count);
            }

            output.flush();
            output.close();
            input.close();
            mProgressDialog.setProgress(0);
            mProgressDialog.dismiss();

        } catch (Exception e) {
        }
        return null;
    }

    {

    }

    @Override
    protected void onPostExecute(String unused) {

        File file = new File(Environment.getExternalStorageDirectory()
                + "/Android/Data/"
                + getApplicationContext().getPackageName()
                + "/files/" + "xxxxxxxx.pdf");
        Intent testIntent = new Intent(Intent.ACTION_VIEW);
        testIntent.setType("application/pdf");
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        Uri uri = Uri.fromFile(file);
        intent.setDataAndType(uri, "application/pdf");
        try {
            startActivity(intent);
        } catch (ActivityNotFoundException e) {
            Toast.makeText(atcChoice.this,
                    "No PDF viewer is available, please download one from Play store",
                    Toast.LENGTH_LONG).show();

        }
    }

}

也许这是一个愚蠢的问题,但文件名不是包含在URL中吗?例如:http://www.blabla.com/my_pdf.pdf - Entreco
我的问题是我一直指定文件名,不确定应该更改哪个部分以允许下载原始文件名并将其保存为该名称。你知道我应该调整什么来实现这个吗? - user1363871
1个回答

11

尝试使用URLUtil.guessFileName()方法。

以下是一个简单的使用示例:

String url = http://...;
String fileExtenstion = MimeTypeMap.getFileExtensionFromUrl(url);
String name = URLUtil.guessFileName(url, null, fileExtenstion);

你能给我演示一下如何调整这段代码以使用URLUtil.guessFileName()吗?我自己尝试过,但它无法正常运行。 - user1363871
我已经添加了一个简单的示例,展示如何使用它。 - avimak

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接