0

This is the code I wrote for downloading image from url, but receving response code 400 with java.io.FilenotFoundException

@Override
protected Void doInBackground(String... strings) {

    try {
        URL url = new URL(strings[0]);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestProperty("User-Agent", "Mozilla/5.0 
        (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) 
        Gecko/20100316 Firefox/3.6.2");
        con.setRequestMethod("GET");
        con.setDoOutput(true);
        con.connect();

        File file = Environment.getExternalStorageDirectory();
        File f1 = new File(file, "_Url download");
        if(!f1.exists()){
            f1.mkdir();
        }

        fileName = System.currentTimeMillis() + ".jpg";

        File f2 = new File(f1, fileName);
        f2.createNewFile();

        InputStream er = con.getErrorStream();
        Log.i("ErrorCode", con.getResponseCode()+"");
        Log.i("ErrorMessage", con.getResponseMessage());
        Log.i("ErrorStream", er+"");

        InputStream in = con.getInputStream();
        FileOutputStream out = new FileOutputStream(f2);

        byte[] buffer = new byte[1024];
        int len;

        System.out.println(Arrays.toString(buffer));

        while((len = in.read(buffer, 0, 1024)) > 0) {
            out.write(buffer, 0, len);
        }

        out.close();
        in.close();

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (ProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        Log.i("IOException", e.toString());
    }
    return null;
} 

LOG

2021-06-26 09:26:25.532 26760-26890/com.example.urldownload I/ErrorCode: 400

2021-06-26 09:26:25.533 26760-26890/com.example.urldownload I/ErrorMessage: Bad Request

2021-06-26 09:26:25.533 26760-26890/com.example.urldownload I/Errorstream: buffer(com.android.okhttp.internal.http.Http1xStream$FixedLengthSource@fbb2c70).inputStream()

2021-06-26 09:26:25.534 26760-26890/com.example.urldownload I/IOException: java.io.FileNotFoundException: https://instagram.fidr1-1.fna.fbcdn.net/v/t51.2885-15/e35/190664842_184685183538740_5039921250568173600_n.jpg?tp=1&_nc_ht=instagram.fidr1-1.fna.fbcdn.net&_nc_cat=108&_nc_ohc=RrEU4lTwYCwAX-vgVQ4&edm=AABBvjUBAAAA&ccb=7-4&oh=3ac34be54793fa59134380fd9e0bd617&oe=60DCB7E6&_nc_sid=83d603

Manifest

<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>

Image of the file where the image is being saved

For more details look at this image

what should I do to resolve this error or is there any better way to do this

Zoe
  • 27,060
  • 21
  • 118
  • 148

3 Answers3

1

I got my mistake. Thank you everyone for sharing your ideas

con.setDoOutput(true); is a POST method And it doesn't fetches any data

con.setDoOutput(true); should not be used.

  • Absolutely the same problem on my end. Having setDoOutput(true) was ruining the download process for me, returning 400 Bad request for all the URL connections. Thank you! – punov Feb 07 '22 at 01:56
0

Use picasso to save images in external storage, you can do something like following

  private Target mTarget = new Target() {
  @Override
  public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
      // Perform simple file operation to store this bitmap
  }

  @Override
  public void onBitmapFailed(Drawable errorDrawable) {
  }

  @Override
  public void onPrepareLoad(Drawable placeHolderDrawable) {
  }
  }

...

Picasso.with(this).load("url").into(mTarget);

Here "Target" is a class provided by picasso, and it has very simple method to understand... This is a easy way to do

Manan jain
  • 111
  • 1
  • 1
  • 6
0

It is hard to figure out what might be causing the error due to lack of provided code. My best guess would be to use DownloadManager instead of AsyncTask.

The download manager is a system service that handles long-running HTTP downloads. Clients may request that a URI be downloaded to a particular destination file. The download manager will conduct the download in the background, taking care of HTTP interactions and retrying downloads after failures or across connectivity changes and system reboots. (https://developer.android.com/reference)

Sai H.
  • 66
  • 5