对话框中的android imageview(URL图像)
这是我的密码:对话框中的android imageview(URL图像),android,Android,这是我的密码: 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.Activ
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_启动器,则可以。但是,如果我想通过URL显示图像,则不会显示图像。
我的代码出了什么问题?您是否检查了LogCat以查看在尝试下载映像时是否引发了任何异常?另外,您针对这个示例的安卓版本是什么?如果您的目标是3.X.X或更高版本(ICS),那么我几乎可以肯定LogCat会显示系统悄悄地抛出NetworkOnMainThreadException,因为您试图从UI线程下载图像,从Honeycom开始不再合法。它不会杀死你的应用程序,但也不会出去下载你的图像。在较新版本的Android中,网络访问需要移动到工作线程,这样网络访问就不会阻塞主线程。因此,您需要使用异步任务。下面是一篇描述我所说内容的文章:
如果不是这样的话,您能发布LogCat中抛出的任何异常吗?主要问题是我忘了添加
<uses-permission android:name="android.permission.INTERNET" />
那么我建议你在这里发布之前开始检查LogCat,因为它会非常清楚地告诉你,你正在尝试使用一个你没有被授权访问的功能。你是否在IE中点击了url或其他验证你正在获取图像的地方?看起来您的图像url是https,但您只使用了HTTPURLConnection,这不应该是HTTPSURLConnection吗?使用http://chart...
asimageFileURL
value