-1

I created an application to download image file from url, but there isin't any data in the file

Image URL: 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

The code:

@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;
}

Result:

Image of file path where the file is being saved

EDIT

Download Button:

downloadButton.setOnClickListener(v -> {
            if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)  != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 101);
            }
            if(ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)  != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 102);
            }
            else if(ContextCompat.checkSelfPermission(this, Manifest.permission.INTERNET)  != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.INTERNET}, 103);
            }
            else {
                BackgroundTask task = new BackgroundTask(this);
                task.execute(et.getText().toString());
            }
        });

Manifest Permissions:

<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"/>

EDIT2

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
Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
  • First things first, you have a space in your file-name. Probably not what you want. System.currentTimeMillis() + " .jpg" – Maverick Jun 25 '21 at 09:50
  • Maybe you don't have rights to write to external storage? Check permissions – Autocrab Jun 25 '21 at 09:58
  • Using your code as is, I am able to download the image file. Can you make sure that the background task is running as expected? Also, you are eating up all the exceptions. If there are any errors you won't know about them. Probably translate them up to the caller so that you have visibility into background threads. – Maverick Jun 25 '21 at 10:02
  • @Maverick Why does throws java.io.FileNotFound Exception for the url which is working fine. – Vivek Singh Jun 25 '21 at 14:01
  • Sorry, did not understand your comment. Is it throwing FileNotFound in the background thread? – Maverick Jun 25 '21 at 16:05
  • Also, please share the output of `con.getResponseCode`. Check method 'getErrorStream' (https://docs.oracle.com/javase/8/docs/api/java/net/HttpURLConnection.html). – Maverick Jun 25 '21 at 16:13
  • @Maverick please have a look at **Edit2** – Vivek Singh Jun 26 '21 at 04:11

1 Answers1

0

Have a look at this SO answer. It seems setDoOutput(true) changes request method to POST.

Maverick
  • 146
  • 8