Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 未找到SD卡上的下载和保存PDF_Java_Android_Pdf - Fatal编程技术网

Java 未找到SD卡上的下载和保存PDF

Java 未找到SD卡上的下载和保存PDF,java,android,pdf,Java,Android,Pdf,我正在尝试从url下载pdf,然后保存并阅读它。但是当我尝试读取时,我发现错误java.io.IOException:/storage/emulated/0/pdf/menu.pdf未作为文件或资源找到。。有什么想法吗 这是我的下载类: public class Downloader { public static void DownloadFile(String fileURL, File directory) { try { FileOut

我正在尝试从url下载pdf,然后保存并阅读它。但是当我尝试读取时,我发现错误
java.io.IOException:/storage/emulated/0/pdf/menu.pdf未作为文件或资源找到。
。有什么想法吗

这是我的下载类:

public class Downloader {

    public static void DownloadFile(String fileURL, File directory) {
        try {

            FileOutputStream f = new FileOutputStream(directory);
            URL u = new URL(fileURL);
            HttpURLConnection c = (HttpURLConnection) u.openConnection();
            c.setRequestMethod("GET");
            c.setDoOutput(true);
            c.connect();

            InputStream in = c.getInputStream();

            byte[] buffer = new byte[1024];
            int len1 = 0;
            while ((len1 = in.read(buffer)) > 0) {
                f.write(buffer, 0, len1);
            }
            f.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
下载和阅读pdf的行:

String extStorageDirectory = Environment.getExternalStorageDirectory().getAbsolutePath()
            .toString();
    File folder = new File(extStorageDirectory, "pdf");
    folder.mkdir();
    File file = new File(folder, "menu.pdf");
    try {
        file.createNewFile();
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    Downloader.DownloadFile("http://webdfd.mines-ales.fr/restau/Menu_Semaine.pdf", file);
    PdfReader reader = new PdfReader(Environment.getExternalStorageDirectory().getAbsolutePath()+"/pdf/menu.pdf");
以下是错误:

    05-02 13:59:01.138 16747-16747/? I/art: Not late-enabling -Xcheck:jni (already on)    
05-02 13:59:01.139 16747-16747/? W/art: Unexpected CPU variant for X86 using defaults: x86    
05-02 13:59:01.676 16747-16747/com.example.yohannmbp.ematoufaire I/InstantRun: starting instant run server: is main process    
05-02 13:59:01.868 16747-16747/com.example.yohannmbp.ematoufaire W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
05-02 13:59:02.440 16747-16787/com.example.yohannmbp.ematoufaire D/NetworkSecurityConfig: No Network Security Config specified, using platform default
05-02 13:59:02.783 16747-16795/com.example.yohannmbp.ematoufaire I/OpenGLRenderer: Initialized EGL, version 1.4
05-02 13:59:02.783 16747-16795/com.example.yohannmbp.ematoufaire D/OpenGLRenderer: Swap behavior 1
05-02 13:59:02.783 16747-16795/com.example.yohannmbp.ematoufaire W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
05-02 13:59:02.783 16747-16795/com.example.yohannmbp.ematoufaire D/OpenGLRenderer: Swap behavior 0
05-02 13:59:03.029 16747-16747/com.example.yohannmbp.ematoufaire W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
05-02 13:59:11.011 16747-16747/com.example.yohannmbp.ematoufaire W/System.err: java.io.IOException: No such file or directory
05-02 13:59:11.012 16747-16747/com.example.yohannmbp.ematoufaire W/System.err:     at java.io.UnixFileSystem.createFileExclusively0(Native Method)
05-02 13:59:11.012 16747-16747/com.example.yohannmbp.ematoufaire W/System.err:     at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:280)
05-02 13:59:11.012 16747-16747/com.example.yohannmbp.ematoufaire W/System.err:     at java.io.File.createNewFile(File.java:948)
05-02 13:59:11.012 16747-16747/com.example.yohannmbp.ematoufaire W/System.err:     at com.example.yohannmbp.ematoufaire.MenuFragment.onCreateView(MenuFragment.java:106)
05-02 13:59:11.012 16747-16747/com.example.yohannmbp.ematoufaire W/System.err:     at android.support.v4.app.Fragment.performCreateView(Fragment.java:2192)
05-02 13:59:11.012 16747-16747/com.example.yohannmbp.ematoufaire W/System.err:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299)
05-02 13:59:11.012 16747-16747/com.example.yohannmbp.ematoufaire W/System.err:     at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
05-02 13:59:11.012 16747-16747/com.example.yohannmbp.ematoufaire W/System.err:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
05-02 13:59:11.012 16747-16747/com.example.yohannmbp.ematoufaire W/System.err:     at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:758)
05-02 13:59:11.012 16747-16747/com.example.yohannmbp.ematoufaire W/System.err:     at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363)
05-02 13:59:11.012 16747-16747/com.example.yohannmbp.ematoufaire W/System.err:     at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149)
05-02 13:59:11.012 16747-16747/com.example.yohannmbp.ematoufaire W/System.err:     at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
05-02 13:59:11.012 16747-16747/com.example.yohannmbp.ematoufaire W/System.err:     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013)
05-02 13:59:11.012 16747-16747/com.example.yohannmbp.ematoufaire W/System.err:     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:710)
05-02 13:59:11.012 16747-16747/com.example.yohannmbp.ematoufaire W/System.err:     at android.os.Handler.handleCallback(Handler.java:751)
05-02 13:59:11.013 16747-16747/com.example.yohannmbp.ematoufaire W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
05-02 13:59:11.013 16747-16747/com.example.yohannmbp.ematoufaire W/System.err:     at android.os.Looper.loop(Looper.java:154)
05-02 13:59:11.013 16747-16747/com.example.yohannmbp.ematoufaire W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6119)
05-02 13:59:11.013 16747-16747/com.example.yohannmbp.ematoufaire W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
05-02 13:59:11.013 16747-16747/com.example.yohannmbp.ematoufaire W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
05-02 13:59:11.013 16747-16747/com.example.yohannmbp.ematoufaire W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
05-02 13:59:11.015 16747-16747/com.example.yohannmbp.ematoufaire W/System.err: java.io.FileNotFoundException: /storage/emulated/0/pdf/menu.pdf (No such file or directory)
05-02 13:59:11.015 16747-16747/com.example.yohannmbp.ematoufaire W/System.err:     at java.io.FileOutputStream.open(Native Method)
05-02 13:59:11.016 16747-16747/com.example.yohannmbp.ematoufaire W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
05-02 13:59:11.016 16747-16747/com.example.yohannmbp.ematoufaire W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:169)
05-02 13:59:11.016 16747-16747/com.example.yohannmbp.ematoufaire W/System.err:     at com.example.yohannmbp.ematoufaire.Downloader.DownloadFile(Downloader.java:18)
05-02 13:59:11.016 16747-16747/com.example.yohannmbp.ematoufaire W/System.err:     at com.example.yohannmbp.ematoufaire.MenuFragment.onCreateView(MenuFragment.java:110)
05-02 13:59:11.017 16747-16747/com.example.yohannmbp.ematoufaire W/System.err:     at android.support.v4.app.Fragment.performCreateView(Fragment.java:2192)
05-02 13:59:11.017 16747-16747/com.example.yohannmbp.ematoufaire W/System.err:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299)
05-02 13:59:11.017 16747-16747/com.example.yohannmbp.ematoufaire W/System.err:     at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
05-02 13:59:11.017 16747-16747/com.example.yohannmbp.ematoufaire W/System.err:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
05-02 13:59:11.018 16747-16747/com.example.yohannmbp.ematoufaire W/System.err:     at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:758)
05-02 13:59:11.018 16747-16747/com.example.yohannmbp.ematoufaire W/System.err:     at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363)
05-02 13:59:11.018 16747-16747/com.example.yohannmbp.ematoufaire W/System.err:     at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149)
05-02 13:59:11.018 16747-16747/com.example.yohannmbp.ematoufaire W/System.err:     at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
05-02 13:59:11.018 16747-16747/com.example.yohannmbp.ematoufaire W/System.err:     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013)
05-02 13:59:11.018 16747-16747/com.example.yohannmbp.ematoufaire W/System.err:     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:710)
05-02 13:59:11.018 16747-16747/com.example.yohannmbp.ematoufaire W/System.err:     at android.os.Handler.handleCallback(Handler.java:751)
05-02 13:59:11.018 16747-16747/com.example.yohannmbp.ematoufaire W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
05-02 13:59:11.018 16747-16747/com.example.yohannmbp.ematoufaire W/System.err:     at android.os.Looper.loop(Looper.java:154)
05-02 13:59:11.018 16747-16747/com.example.yohannmbp.ematoufaire W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6119)
05-02 13:59:11.018 16747-16747/com.example.yohannmbp.ematoufaire W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
05-02 13:59:11.018 16747-16747/com.example.yohannmbp.ematoufaire W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
05-02 13:59:11.018 16747-16747/com.example.yohannmbp.ematoufaire W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
05-02 13:59:11.088 16747-16752/com.example.yohannmbp.ematoufaire I/art: Do partial code cache collection, code=30KB, data=29KB
05-02 13:59:11.089 16747-16752/com.example.yohannmbp.ematoufaire I/art: After code cache collection, code=23KB, data=25KB
05-02 13:59:11.089 16747-16752/com.example.yohannmbp.ematoufaire I/art: Increasing code cache capacity to 128KB
05-02 13:59:11.127 16747-16747/com.example.yohannmbp.ematoufaire I/System.out: java.io.IOException: /storage/emulated/0/pdf/menu.pdf not found as file or resource.
05-0213:59:01.138 16747-16747/?I/art:未延迟启用-Xcheck:jni(已启用)
05-02 13:59:01.139 16747-16747/? W/art:X86的意外CPU变量使用默认值:X86
05-02 13:59:01.676 16747-16747/com.example.yohannmbp.ematoufire I/instanttrun:启动即时运行服务器:是主进程
05-02 13:59:01.868 16747-16747/com.example.yohannmbp.ematoufire W/art:Android 4.1之前的方法Android.graphics.PorterDuffColorFilter Android.support.graphics.drawable.VectorDrawableCompat.updatetingFilter(Android.graphics.PorterDuffColorFilter,Android.content.res.ColorStateList,Android.graphics.PorterDuff$)将错误地重写android.graphics.drawable.drawable中的包私有方法
05-02 13:59:02.440 16747-16787/com.example.yohannmbp.ematoufire D/NetworkSecurityConfig:未指定网络安全配置,使用平台默认值
05-02 13:59:02.783 16747-16795/com.example.yohannmbp.ematoufire I/OpenGLRenderer:初始化EGL,版本1.4
05-02 13:59:02.783 16747-16795/com.example.yohannmbp.ematoufire D/OpenGLRenderer:Swap行为1
05-02 13:59:02.783 16747-16795/com.example.yohannmbp.ematoufire W/OpenGLRenderer:未能选择保留EGL交换行为的配置,正在重试,但没有。。。
05-02 13:59:02.783 16747-16795/com.example.yohannmbp.ematoufire D/OpenGLRenderer:Swap行为0
05-02 13:59:03.029 16747-16747/com.example.yohannmbp.ematoufire W/art:在Android 4.1之前,Android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int,boolean)中的方法int会错误地覆盖Android.widget.ListView中的包私有方法
05-02 13:59:11.011 16747-16747/com.example.yohannmbp.ematoufire W/System.err:java.io.IOException:没有这样的文件或目录
05-02 13:59:11.012 16747-16747/com.example.yohannmbp.ematoufire W/System.err:at java.io.UnixFileSystem.createFileExclusively0(本机方法)
05-02 13:59:11.012 16747-16747/com.example.yohannmbp.ematoufire W/System.err:at java.io.UnixFileSystem.createfileexclusive(UnixFileSystem.java:280)
05-02 13:59:11.012 16747-16747/com.example.yohannmbp.ematoufire W/System.err:at java.io.File.createNewFile(File.java:948)
05-02 13:59:11.012 16747-16747/com.example.yohannmbp.ematoufire W/System.err:at com.example.yohannmbp.ematoufire.menuframent.onCreateView(menuframent.java:106)
05-02 13:59:11.012 16747-16747/com.example.yohannmbp.ematoufire W/System.err:at-android.support.v4.app.Fragment.performCreateView(Fragment.java:2192)
05-02 13:59:11.012 16747-16747/com.example.yohannmbp.ematoufire W/System.err:at-android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299)
05-02 13:59:11.012 16747-16747/com.example.yohannmbp.ematoufire W/System.err:at-android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
05-02 13:59:11.012 16747-16747/com.example.yohannmbp.ematoufire W/System.err:at-android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
05-02 13:59:11.012 16747-16747/com.example.yohannmbp.ematoufire W/System.err:at-android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:758)
05-02 13:59:11.012 16747-16747/com.example.yohannmbp.ematoufire W/System.err:at-android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363)
05-02 13:59:11.012 16747-16747/com.example.yohannmbp.ematoufire W/System.err:at-android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149)
05-02 13:59:11.012 16747-16747/com.example.yohannmbp.ematoufire W/System.err:at-android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
05-02 13:59:11.012 16747-16747/com.example.yohannmbp.ematoufire W/System.err:at-android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013)
05-02 13:59:11.012 16747-16747/com.example.yohannmbp.ematoufire W/System.err:at-android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:710)
05-02 13:59:11.012 16747-16747/com.example.yohannmbp.ematoufire W/System.err:at android.os.Handler.handleCallback(Handler.java:751)
05-02 13:59:11.013 16747-16747/com.example.yohannmbp.ematoufire W/System.err:at-android.os.Handler.dispatchMessage(Handler.java:95)
05-02 13:59:11.013 16747-16747/com.example.yohannmbp.ematoufire W/System.err:at-android.os.Looper.loop(Looper.java:154)
05-02 13:59:11.013 16747-16747/com.example.yohannmbp.ematoufire W/System.err:位于android.app.ActivityThread.main(ActivityThread.java:6119)
05-02 13:59:11.013 16747-16747/com.example.yohannmbp.ematoufire W/System.err:at java.lang.reflect.Method.invoke(本机方法)
05-02 13:59:11.013 16747-16747/com.example.yohannmbp.ematoufire W/System.err:com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
05-02 13:59:11.013 16747-16747/com.example.yohannmbp.ematoufire W/System.err:at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
05-02 13:59:11.015 16747-16747/com.example.yohannmbp.ematoufire W/System.err:java.io.FileNotFoundException:/storage/emulated/0/pdf/menu.pdf(无此类文件或目录)
05-02 13:59:11.015 16747-16747/com.example.yohannmbp.ematoufire W/System.err:at java.io.FileOutputStream.open(本机方法)
05-02 13:59:11.016 16747-16747/com.example.yohannmbp.ematoufire W/System.err:at java.io.FileOutputStream.(FileOutputStream.java:221)
05-02 13:59:11.016 16747-16747/com.example.yohannmbp.ematoufire W/System.err:at java.io.FileOutputStream.(FileOutputStream.j
downloadTask = new DownloadTask();
            downloadTask.execute("http://webdfd.mines-ales.fr/restau/Menu_Semaine.pdf", "", "");

private class DownloadTask extends AsyncTask<String, Integer, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            ll_view.setVisibility(View.VISIBLE);

        }

        @Override
        protected String doInBackground(String... sUrl) {
            InputStream input = null;
            OutputStream output = null;
            HttpURLConnection connection = null;
            try {
                URL url = new URL(sUrl[0]);
                connection = (HttpURLConnection) url.openConnection();
                connection.connect();

                // expect HTTP 200 OK, so we don't mistakenly save error report
                // instead of the file
                if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
                    return "Server returned HTTP " + connection.getResponseCode()
                            + " " + connection.getResponseMessage();
                }

                // this will be useful to display download percentage
                // might be -1: server did not report the length
                int fileLength = connection.getContentLength();

                // download the file
                input = connection.getInputStream();

                File sourcePath = Environment.getExternalStorageDirectory();

                File path = new File(sourcePath + "/" + Constants.DIR_NAME + "/");

                path.mkdir();

                File file = new File(path, "/ecute("http://webdfd.mines-ales.fr/restau/Menu_Semaine.pdf");
                if (file.exists()) {
                    file.delete();
                }
                output = new FileOutputStream(file);

                byte data[] = new byte[4096];
                long total = 0;
                int count;
                while ((count = input.read(data)) != -1) {
                    // allow canceling with back button
                    if (isCancelled()) {
                        input.close();
                        return null;
                    }
                    total += count;
                    // publishing the progress....
                    if (fileLength > 0) // only if total length is known
                        publishProgress((int) (total * 100 / fileLength));
                    output.write(data, 0, count);
                }
            } catch (Exception e) {
                return e.toString();
            } finally {
                try {
                    if (output != null)
                        output.close();
                    if (input != null)
                        input.close();
                } catch (IOException ignored) {
                }

                if (connection != null)
                    connection.disconnect();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            ll_view.setVisibility(View.GONE);


        }

        @Override
        protected void onProgressUpdate(final Integer... values) {
            super.onProgressUpdate(values);
//            Log.e("progressing", "" + values[0]);
//            edt_search.setText("" + values[0]);
//            new Thread(new Runnable() {
//                public void run() {
            progressBar.setProgress(values[0]);
//            tv_downloading.setText("" + values[0] + "%");
//                }
//            }).start();


        }

        @Override
        protected void onCancelled(String s) {
            super.onCancelled(s);
        }

        @Override
        protected void onCancelled() {
            super.onCancelled();
        }
    }
private class DownloadTask extends AsyncTask<String, Integer, String> {
        private Context context;
        private PowerManager.WakeLock mWakeLock;

        public DownloadTask(Context context) {
        this.context = context;
    }

**strong text**    @Override
    protected String doInBackground(String... sUrl) {
        InputStream input = null;
        OutputStream output = null;
        HttpURLConnection connection = null;
        try {
            URL url = new URL(AppConstant.BASE_URL + URLEncoder.encode(sUrl[0], "UTF-8"));
            connection = (HttpURLConnection) url.openConnection();
            connection.connect();

            // expect HTTP 200 OK, so we don't mistakenly save error report
            // instead of the file
            if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
                return "Server returned HTTP " + connection.getResponseCode()
                        + " " + connection.getResponseMessage();
            }

            // this will be useful to display download percentage
            // might be -1: server did not report the length
            int fileLength = connection.getContentLength();

            // download the file
            fileName = sUrl[0].substring(sUrl[0].lastIndexOf("/") + 1,
                    sUrl[0].length());
            input = connection.getInputStream();
            output = new FileOutputStream(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/" + fileName);

            byte data[] = new byte[4096];
            long total = 0;
            int count;
            while ((count = input.read(data)) != -1) {
                // allow canceling with back button
                if (isCancelled()) {
                    input.close();
                    return null;
                }
                total += count;
                // publishing the progress....
                if (fileLength > 0) // only if total length is known
                    publishProgress((int) (total * 100 / fileLength));
                output.write(data, 0, count);
            }
        } catch (Exception e) {
            return e.toString();
        } finally {
            try {
                if (output != null)
                    output.close();
                if (input != null)
                    input.close();
            } catch (IOException ignored) {
            }

            if (connection != null)
                connection.disconnect();
        }
        return null;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // take CPU lock to prevent CPU from going off if the user
        // presses the power button during download
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                getClass().getName());
        mWakeLock.acquire();
        mProgressDialog.show();
    }

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

    @Override
    protected void onPostExecute(String result) {
        mWakeLock.release();
        mProgressDialog.dismiss();
        if (result != null)
            Toast.makeText(context, "Download error: File is not Found.", Toast.LENGTH_LONG).show();
        else {
            new AlertDialog.Builder(context)
                    //.setTitle("Delete entry")
                    .setMessage("File Downloaded Successfully. Do you want to open it?")
                    .setPositiveButton("Open", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            try {
                                File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/" + fileName);
                                MimeTypeMap map = MimeTypeMap.getSingleton();
                                String ext = MimeTypeMap.getFileExtensionFromUrl(file.getName());
                                String type = map.getMimeTypeFromExtension(ext);

                                if (type == null)
                                    type = "*/*";

                                Intent intent = new Intent(Intent.ACTION_VIEW);
                                Uri data = Uri.fromFile(file);

                                intent.setDataAndType(data, type);

                                context.startActivity(intent);
                            } catch (ActivityNotFoundException e) {
                                Toast.makeText(context, "There is no app registered to handle the type of file selected.", Toast.LENGTH_SHORT).show();
                            }

                        }
                    })
                    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            // do nothing
                        }
                    })
                    .setIcon(android.R.drawable.ic_dialog_alert)
                    .show();
            Toast.makeText(context, "File downloaded", Toast.LENGTH_SHORT).show();
        }
    }
}