1

I'm developing an android project which it works with instagram api.

I handle everything I need but there is a little problem. I can get an image url and I test it in my laptop browser and it shows the picture, but when I try to to download the picture, I receive an exception which its message is my url link

My exception:

09-06 18:11:14.968: W/System.err(5434): java.io.FileNotFoundException: http://photos-b.ak.instagram.com/hphotos-ak-xaf1/t51.2885-15/914487_1464108403854853_909913710_s.jpg 09-06 18:11:14.968: W/System.err(5434): at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:186) 09-06 18:11:14.978: W/System.err(5434): at com.watermelonco.hashtagram.Connection.ImageDownloader.downloadImage(ImageDownloader.java:123) 09-06 18:11:14.978: W/System.err(5434): at com.watermelonco.hashtagram.Connection.ImageDownloader.doInBackground(ImageDownloader.java:47) 09-06 18:11:14.978: W/System.err(5434): at com.watermelonco.hashtagram.Connection.ImageDownloader.doInBackground(ImageDownloader.java:1) 09-06 18:11:14.978: W/System.err(5434): at android.os.AsyncTask$2.call(AsyncTask.java:288) 09-06 18:11:14.978: W/System.err(5434): at java.util.concurrent.FutureTask.run(FutureTask.java:237) 09-06 18:11:14.978: W/System.err(5434): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 09-06 18:11:14.978: W/System.err(5434): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 09-06 18:11:14.978: W/System.err(5434): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 09-06 18:11:14.978: W/System.err(5434): at java.lang.Thread.run(Thread.java:841)

here is my downloading code

private void downloadImage(String url_link)
{
    HttpURLConnection urlConnection = null;
    FileOutputStream fileOutput = null;
    InputStream inputStream = null;

    try
    {
        // There is nothing wrong with file address
        File file = new File(Picture.getFolderPath(activity), picture.getThumbNailName());  

        if (file.exists() == false || file.length() < 10)
        {
            file.createNewFile();
        }
        else
        {
            return;
        }

        URL url = new URL(url_link);

        urlConnection = (HttpURLConnection) url.openConnection();

        urlConnection.setRequestMethod("GET");
        urlConnection.setDoOutput(true);
        urlConnection.connect();

        fileOutput = new FileOutputStream(file);
        inputStream = urlConnection.getInputStream();

        Log.e(MainActivity.WATERMELON_TAG, "Downloading: " + url_link);

        byte[] buffer = new byte[1024];
        int bufferLength = 0;

        while ((bufferLength = inputStream.read(buffer)) > 0)
        {
            fileOutput.write(buffer, 0, bufferLength);
        }
    }
    catch (Exception e)
    {
        Log.e(MainActivity.WATERMELON_TAG, "Exception! " + e.getMessage());
    }
    finally
    {
        if (fileOutput != null)
        {
            try
            {
                fileOutput.close();
            }
            catch (IOException e)
            {
            }
        }

        if (inputStream != null)
        {
            try
            {
                inputStream.close();
            }
            catch (IOException e)
            {
            }
        }

        if (urlConnection != null)
        {
            try
            {
                urlConnection.disconnect();
            }
            catch (Exception e)
            {
            }
        }
    }
}

and for the url_link I set

http://photos-b.ak.instagram.com/hphotos-ak-xaf1/t51.2885-15/914487_1464108403854853_909913710_s.jpg

that it's actually a picture. And I have added the internet permission in my project

What's wrong with this?

Thanks in advance

strings95
  • 661
  • 11
  • 26

0 Answers0