0

Hi every one I am trying to download pdf from url but it is throwing exception java.io.FileNotFoundException:url but with same code I am downloading image it is working fine I am trying to solve it for last three days but not able to do so any help much appreciated

 protected String doInBackground(String... params)
{
    int count;
    try
    {
        URL url = new URL(params[0]);
        URLConnection conection = url.openConnection();
        conection.connect();

        InputStream input = null;


        input = new BufferedInputStream(conection.getInputStream());

        File mFolder = new File("" + context.getFilesDir());
        if (!mFolder.exists())
            mFolder.mkdirs();
        File fileData = new File(mFolder.getAbsolutePath() + "/" + "myfile"+id);
        if (!fileData.exists())
            fileData.createNewFile();
        OutputStream output = new FileOutputStream(context.getFilesDir() + "/" + media.id);

        input = url.openStream();
        byte[] data = new byte[(int) (media.size + 2)];
        int totalBytes=0;
        while ((count = input.read(data)) != -1)
        {
            totalBytes+=count;
            output.write(data, 0, count);
        }
        if(totalBytes == media.size)
        {
            Log.e("Downloaded Bytes",String.valueOf(totalBytes));
        }
        // flushing output
        output.flush();
        // closing streams
        output.close();
        input.close();

    }
    catch (Exception e)
    {
        e.printStackTrace();
        isSuccess=false;
    }
    return null;
}

it is throwing exception from input = url.openStream()

Ashish Pandya
  • 167
  • 10

1 Answers1

1
input = url.openStream();. 

Remove that line. You have already an input

greenapps
  • 11,154
  • 2
  • 16
  • 19