3

试图让这个简单的下载和保存图像工作,但我不断收到连接超时异常。

据我所知,网址应该可以使用

new DownloadImageTask();



private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {


    protected Bitmap doInBackground(String... urls) {

        String urldisplay = "http://masterzangetsu.eu/Apps/BandWallpapers/Blink182/1_thumbnail.png";
        Bitmap bitmap = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            bitmap = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return bitmap;
    }

    @Override
    protected void onPreExecute() {

    }

    @Override
    protected void onProgressUpdate(Void... values) {

    }

    protected void onPostExecute(Bitmap bitmap) {

        File sdCardDirectory = Environment.getExternalStorageDirectory();
        File image = new File(sdCardDirectory, "test.png");

        boolean success = false;

        // Encode the file as a PNG image.
        FileOutputStream outStream;
        try {

            outStream = new FileOutputStream(image);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream); 
            /* 100 to keep full quality of the image */

            outStream.flush();
            outStream.close();
            success = true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (success) {
            Toast.makeText(getApplicationContext(), "Image saved with success",
                    Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(getApplicationContext(),
                    "Error during image saving", Toast.LENGTH_LONG).show();
        }
    }
}

这是输出

10-29 12:33:36.075: W/System.err(817): java.net.ConnectException: failed to connect to     masterzangetsu.eu/91.208.99.12 (port 80): connect failed: ETIMEDOUT (Connection timed out)
10-29 12:33:36.075: W/System.err(817):  at libcore.io.IoBridge.connect(IoBridge.java:114)
10-29 12:33:36.095: W/System.err(817):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
10-29 12:33:36.095: W/System.err(817):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
10-29 12:33:36.095: W/System.err(817):  at java.net.Socket.connect(Socket.java:842)
10-29 12:33:36.095: W/System.err(817):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:76)
10-29 12:33:36.095: W/System.err(817):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
10-29 12:33:36.095: W/System.err(817):  at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
10-29 12:33:36.095: W/System.err(817):  at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
10-29 12:33:36.105: W/System.err(817):  at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
10-29 12:33:36.105: W/System.err(817):  at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
10-29 12:33:36.105: W/System.err(817):  at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
10-29 12:33:36.105: W/System.err(817):  at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
10-29 12:33:36.115: W/System.err(817):  at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
10-29 12:33:36.115: W/System.err(817):  at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:282)
10-29 12:33:36.115: W/System.err(817):  at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:495)
10-29 12:33:36.115: W/System.err(817):  at com.koushikdutta.urlimageviewhelper.HttpUrlDownloader$1.doInBackground(HttpUrlDownloader.java:51)
10-29 12:33:36.115: W/System.err(817):  at com.koushikdutta.urlimageviewhelper.HttpUrlDownloader$1.doInBackground(HttpUrlDownloader.java:1)
10-29 12:33:36.125: W/System.err(817):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-29 12:33:36.125: W/System.err(817):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
10-29 12:33:36.125: W/System.err(817):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
10-29 12:33:36.136: W/System.err(817):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
10-29 12:33:36.136: W/System.err(817):  at java.lang.Thread.run(Thread.java:856)
10-29 12:33:36.145: W/System.err(817): Caused by: libcore.io.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)
10-29 12:33:36.165: W/System.err(817):  at libcore.io.Posix.connect(Native Method)
10-29 12:33:36.165: W/System.err(817):  at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
10-29 12:33:36.165: W/System.err(817):  at     libcore.io.IoBridge.connectErrno(IoBridge.java:127)
10-29 12:33:36.165: W/System.err(817):  at libcore.io.IoBridge.connect(IoBridge.java:112)
10-29 12:33:36.165: W/System.err(817):  ... 21 more

完整输出http://pastie.org/8439940

和权限

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

任何帮助都会很棒。

4

1 回答 1

3

我已经测试了您的代码,并且可以成功运行。我建议你检查你的互联网连接。

于 2013-10-29T13:34:50.020 回答