3

I have written this code to download files from URL. It downloaded doc and jpg files. BUt PDF files not downloaded and it shows com.android.volley.ClientError. URL contains this "શુભમ08_23_2016_06_05_33.pdf". Is this problem because of Gujrati characters in url?

This is my Code:

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);
Keyur
  • 27
  • 3
  • 3
    Can you please include the full error LogCat, as specifically the HTTP error code will be of use. – Ken Y-N Aug 29 '16 at 05:06
  • Full Logcat only shows this: com.android.volley.ClientError – Keyur Aug 29 '16 at 05:31
  • I downloaded other PDF file and it worked, that means it did not downloaded because character were in gujarati. So Please tell me how to download this type of file. – Keyur Aug 29 '16 at 05:47
  • Try [this instead](https://stackoverflow.com/questions/4831301/invalid-uri-with-chinese-characters-java) - you are perhaps missing `URL url = new URL(uri.toASCIIString());` somewhere? – Ken Y-N Aug 29 '16 at 07:02

0 Answers0