这是我的代码:
package com.AndroidCustomDialog;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
public class AndroidCustomDialog extends Activity {
static final int CUSTOM_DIALOG_ID = 0;
private String imageFileURL = "https://chart.googleapis.com/chart?cht=lc&chd=s:cEAELFJHHHKUju9uuXUc&chco=76A4FB&chls=2.0,0.0,0.0&chxt=x,y&chxl=0:%7C0%7C1%7C2%7C3%7C4%7C5%7C1:%7C0%7C50%7C100&chs=200x125&chg=20,50";
TextView customDialog_TextView;
EditText customDialog_EditText;
ImageView myImageView;
Button customDialog_Update, customDialog_Dismiss;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1main = (Button) findViewById(R.id.Button01main);
button1main.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Dialog dialog = new Dialog(AndroidCustomDialog.this);
dialog.setContentView(R.layout.customlayout);
dialog.setTitle("This is my custom dialog box");
dialog.setCancelable(true);
TextView text = (TextView) dialog.findViewById(R.id.TextView01);
text.setText("HIHI JORDAN");
ImageView img = (ImageView) dialog.findViewById(R.id.ImageView01);
try {
URL url = new URL(imageFileURL);
URLConnection conn = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection)conn;
httpConn.setRequestMethod("GET");
httpConn.connect();
if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream inputStream = httpConn.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
inputStream.close();
img.setImageBitmap(bitmap);
}
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//img.setImageResource(R.drawable.ic_launcher);
Button button = (Button) dialog.findViewById(R.id.Button01);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});
dialog.show();
}
});
}
}
如果imageview只显示R.drawable.ic_launcher,那就没关系。但是,如果我想通过URL显示图像,则不会显示图像。 我的代码有什么问题?
答案 0 :(得分:1)
主要问题是我忘了添加
<uses-permission android:name="android.permission.INTERNET" />
答案 1 :(得分:1)
然后我建议您在发布之前开始检查LogCat,因为它会非常清楚地告诉您,您试图使用未被授予访问权限的功能。
答案 2 :(得分:0)
您是否检查过LogCat以查看在尝试下载图像时是否有任何异常?此外,您针对此示例的目标是什么版本的Android?如果你的目标是3.XX或更高(ICS),那么我几乎可以肯定LogCat会显示系统悄悄地抛出了NetworkOnMainThreadException,因为你试图从UI Thread下载一个图像,从Honeycomb开始是不再合法。它不会杀死你的应用程序,但它也不会出去下载你的图像。在较新版本的Android中,需要将网络访问权限移动到工作线程,以便网络访问不会阻止主线程。因此,您需要使用AsyncTask。可以在这里找到描述我所谈论内容的文章: http://developer.android.com/resources/articles/painless-threading.html
如果情况并非如此,您是否可以发布LogCat中引发的任何异常?