確保您在AndroidManifest.xml中設置了以下權限來訪問互聯網。
<uses-permission android:name="android.permission.INTERNET" />
在MainActivity.java:
public void onClick(View v) {
// show The Image in a ImageView
new DownloadImageTask((ImageView) findViewById(R.id.imageView1))
.execute("https://chart.googleapis.com/chart?cht=qr&chs=150x150&chl=LG-80294959&choe=UTF-8");
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
享受吧!
謝謝!這解決了Google Charts的問題。 相同格式的另一個URL似乎沒有工作,但是:https://hackforums.net/captcha.php?imagehash=de186516263983d99bdc789691675ae7 它拋出「了java.lang.RuntimeException:執行doInBackground時出錯()「由於InputStream的」java.lang.NullPointerException「。任何想法爲什麼? –