从instagram android下载图像

时间:2014-09-06 13:00:13

标签: android download instagram

我正在开发一个与instagram api一起使用的android项目。

我处理我需要的一切,但有一点问题。我可以获得一个图像网址,我在我的笔记本电脑浏览器中进行测试并显示图片,但是当我尝试下载图片时,我收到一个异常,其中的消息是我的网址链接

我的例外:

  

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)

这是我的下载代码

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)
            {
            }
        }
    }
}

和我设置的url_link

  

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

它实际上是一张照片。 我在我的项目中添加了互联网权限

这有什么问题?

提前致谢

0 个答案:

没有答案