Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Get the original file name for an async download

I would like to retrieve the original file name from a website while downloading a PDF file, not rename it but save it as the same name under the directory SD/Android/Data/(MyAPP)/Files. As of now I have to tell android what the file name should be when looking for it and what to save it as while downloading. Is there a simple way to change this to save it as the original name? Thanks

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

        }
    }

}
like image 448
user1363871 Avatar asked Dec 27 '22 01:12

user1363871


1 Answers

Try using URLUtil.guessFileName()

Here is a simple example of how using it:

String url = http://...;
String fileExtenstion = MimeTypeMap.getFileExtensionFromUrl(url);
String name = URLUtil.guessFileName(url, null, fileExtenstion);
like image 58
avimak Avatar answered Dec 30 '22 12:12

avimak