从网络上传图片

Upload image from Network

我无法从 Internet 下载图片。

public class ImageUploadActivity extends ActionBarActivity {

    private final String myURL = "http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png";
    private ImageView uploadImage;
    Bitmap bitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_upload);

        uploadImage = (ImageView) findViewById(R.id.upload_image);
        bitmap = getBitmapFromURL(myURL);
        uploadImage.setImageBitmap(bitmap);

    }

    private Bitmap getBitmapFromURL(String src) {
        try {
            URL url = new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
        } catch (Exception e) {
            e.getStackTrace();
            return null;
        }
    }
}

没有异常,但我的 ImageView 仍然是空的。我也尝试用 AsyncTask 做这个,但结果是一样的。

02-07 05:04:18.661  30323-30346/com.myapplication4.app D/OpenGLRenderer﹕ Render dirty regions requested: true
02-07 05:04:18.674  30323-30323/com.myapplication4.app D/Atlas﹕ Validating map...
02-07 05:04:18.808  30323-30346/com.myapplication4.app I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:410>: QUALCOMM Build: 10/28/14, c33033c, Ia6306ec328
02-07 05:04:18.811  30323-30346/com.myapplication4.app I/OpenGLRenderer﹕ Initialized EGL, version 1.4
02-07 05:04:18.841  30323-30346/com.myapplication4.app D/OpenGLRenderer﹕ Enabling debug mode 0

我该如何解决这个问题?

这里有 2 件事:

在您的 URL 中使用 https://,如下所示:

private final String myURL ="https://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png";

并且由于您在 App 的主线程上执行 IO,这始终是严格的 NO-NO,因此您的代码将导致 IOException。

如果你调试这段代码,你会发现下面一行有IOException connection.connect();

为了解决这个问题,你应该使用AsyncTask进行IO操作。

下面是使用 Given URL:

下载图片的完整代码
public class MainActivity extends Activity {

    private final String myURL ="https://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png";
    private ImageView uploadImage;
    Bitmap bitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("MainActivity", "onCreate");
        setContentView(R.layout.activity_main);
        uploadImage = (ImageView) findViewById(R.id.imageView1);        
        new MyDownloadTask().execute();        
    }


    class MyDownloadTask extends AsyncTask<Void,Void,Void>
    {
        protected void onPreExecute() {
          //display progress dialog.

     }
        protected Void doInBackground(Void... params) {         
            try {
                URL url = new URL(myURL);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setDoInput(true);
                connection.connect();
                InputStream input = connection.getInputStream();
                Bitmap myBitmap = BitmapFactory.decodeStream(input);
                uploadImage.setImageBitmap(myBitmap);
                connection.disconnect();

            } catch (Exception e) {
                e.getStackTrace();
                return null;
            }
            return null;         
     }
     protected void onPostExecute(Void result) {
         Log.e("MainActivity", "onPostExecute");         
     }
    }   
}

下面是我的示例应用程序 UI 中显示的下载完成后的图像。

只需将此库 (AQuery) 添加到您的项目中。这个库非常轻巧,极大地简化了我的生活。

使用方法如下:

    public class MainActivity extends Activity {

private final String myURL ="https://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png";
private ImageView uploadImage;
Bitmap bitmap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    AQuery aq = new AQuery(this);
    uploadImage = (ImageView) findViewById(R.id.imageView1);        
    aq.id(uploadImage).image(myUrl, false, true);
    //it caches the image so the second time you try to download the image it reads from cache file and  it is gonna be so fast
}
}