来自URL的Downlad PDF

时间:2016-08-29 05:01:33

标签: android file pdf download android-volley

我已编写此代码以从URL下载文件。它下载了doc和jpg文件。 BUt未下载的PDF文件,它显示com.android.volley.ClientError。网址包含此“શુભમ08_23_2016_06_05_33.pdf”。这个问题是因为网址中的古吉拉特字符吗?

这是我的代码:

InputStreamVolleyRequest request = new InputStreamVolleyRequest(Request.Method.GET, mURL,
                        new Response.Listener<byte[]>()
                        {
                            @Override
                            public void onResponse(byte[] response) {
                                // TODO handle the response
                                try {
                                    if (response!=null) {
                                        pd.dismiss();
                                        FileOutputStream outputStream;
                                        Features features=new Features(context);

                                        String name=features.findName(mURL);;//<FILE_NAME_WITH_EXTENSION e.g reference.txt>
                                        File sdCard = Environment.getExternalStorageDirectory();
                                        File dir = new File (sdCard.getAbsolutePath() + "/AIO");
                                        if (!dir.isDirectory())
                                        {
                                            dir.mkdirs();
                                        }

                                        File file = new File(dir, name);

                                        FileOutputStream f = new FileOutputStream(file);
                                        //f=context.openFileOutput(name, Context.MODE_PRIVATE);
                                        f.write(response);
                                        f.close();
                                        features.ToastCenter("File Downloaded in AIO Folder",true);


                                        Intent intent = new Intent(Intent.ACTION_VIEW);
                                        String extension=features.findExtension(mURL);
                                        intent.setDataAndType(Uri.fromFile(file),"application/"+extension);
                                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                        try
                                        {
                                            boolean isFileOpened=false;
                                            try
                                            {
                                                context.startActivity(intent);
                                                isFileOpened=true;
                                            }
                                            catch (Exception e)
                                            {
                                               //Don't show any message here
                                            }

                                            if (!isFileOpened)
                                            {
                                                try
                                                {
                                                    intent.setDataAndType(Uri.fromFile(file),"application/msword");
                                                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                                    context.startActivity(intent);
                                                }
                                                catch (Exception e)
                                                {
                                                    features.ToastCenter("Can not view file",true);
                                                }

                                            }


                                        }
                                        catch (ActivityNotFoundException e)
                                        {


                                        }


                                    }
                                } catch (Exception e) {
                                    // TODO Auto-generated catch block
                                    Log.d("KEY_ERROR", "UNABLE TO DOWNLOAD FILE");
                                    e.printStackTrace();
                                }
                            }
                        } ,new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Features features=new Features(context);
                        features.ToastCenter("Failed to Download File",true);
                        // TODO handle the error
                        error.printStackTrace();
                        pd.dismiss();
                    }
                }, null);
                RequestQueue mRequestQueue = Volley.newRequestQueue(context, new HurlStack());
                mRequestQueue.add(request);

0 个答案:

没有答案