我无法从互联网上下载图片。
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
我该如何解决这个问题?
答案 0 :(得分:0)
这里有2件事:
在您的网址中使用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中显示完成下载后的图像。
答案 1 :(得分:0)
只需将此库(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
}
}