想在android的ImageView里面显示一张网络图片但是图片一直不显示,manifest里面已经配置了访问网络的权限了,我先把代码都附上。
1.这个是GetWebIm.java
public class GetWebIm {
private Bitmap bitmap = null;
public Bitmap getPicBitmap(){ String url = "http://www.baidu.com/img/shouye_b5486898c692066bd2cbaeda86d74448.gif";
try {
URL myFileUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
}
2.这个是MainActivity.java
public class MainActivity extends Activity {
private ImageView im = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
im = (ImageView)this.findViewById(R.id.id);
im.setImageBitmap(new GetWebIm().getPicBitmap());
}}
3.xml文件
<?xml version="1.0" encoding= "utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>android网络图片
1.这个是GetWebIm.java
public class GetWebIm {
private Bitmap bitmap = null;
public Bitmap getPicBitmap(){ String url = "http://www.baidu.com/img/shouye_b5486898c692066bd2cbaeda86d74448.gif";
try {
URL myFileUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
}
2.这个是MainActivity.java
public class MainActivity extends Activity {
private ImageView im = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
im = (ImageView)this.findViewById(R.id.id);
im.setImageBitmap(new GetWebIm().getPicBitmap());
}}
3.xml文件
<?xml version="1.0" encoding= "utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>android网络图片
static ImageView mImageView;
Drawable drawable;
String url = "http://www.baidu.com/img/shouye_b5486898c692066bd2cbaeda86d74448.gif";//诸如此类的中文图片地址
static Bitmap bit;
//Bitmap bitmap ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyView mv = new MyView(this);
// setContentView(mv);
setContentView(R.layout.activity_bitmap);
MyThread myThread = new MyThread();
myThread.start();
// mImageView = (ImageView)this.findViewById(R.id.imageView1) ;
// drawable = loadImageFromNetwork(url );
// mImageView.setImageDrawable(drawable) ;
// mImageView = (ImageView)this.findViewById(R.id.imageView1) ;
// new Thread(new Runnable(){
// Drawable drawable = loadImageFromNetwork(url);
// @Override
// public void run() {
//
// // post() 特别关键,就是到UI主线程去更新图片
// mImageView.post(new Runnable(){
// @Override
// public void run() {
// // TODO Auto-generated method stub
// mImageView.setImageDrawable(drawable) ;
// }}) ;
// }
//
// }).start() ;
mImageView = (ImageView)this.findViewById(R.id.imageView1) ;
new DownloadImageTask().execute(url) ;
}
class DownloadImageTask extends AsyncTask<String, Void, Drawable>
{
protected Drawable doInBackground(String... urls) {
return loadImageFromNetwork(urls[0]);
}
protected void onPostExecute(Drawable result) {
mImageView.setImageDrawable(result);
}
} private Drawable loadImageFromNetwork(String imageUrl)
{
Drawable drawable = null;
try {
// 可以在这里通过文件名来判断,是否本地有此图片
drawable = Drawable.createFromStream(
new URL(imageUrl).openStream(), "image.jpg");
} catch (IOException e) {
Log.d("test", e.getMessage());
}
if (drawable == null) {
Log.d("test", "null drawable");
} else {
Log.d("test", "not null drawable");
}
return drawable ;
}
class MyThread extends Thread{ public void run() {
// while(true){
bit = returnBitMap(url);
handler.sendEmptyMessage(0);
//}
}
}
//定义Handler对象
private Handler handler =new Handler(){
@Override
//当有消息发送出来的时候就执行Handler的这个方法
public void handleMessage(Message msg){
super.handleMessage(msg);
//处理UI
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.bitmap, menu);
return true;
}
public Bitmap returnBitMap(String ur) {
// String url = "http://www.baidu.com/img/shouye_b5486898c692066bd2cbaeda86d74448.gif";//诸如此类的中文图片地址
URL myFileUrl = null;
Bitmap bitmap = null; try {
myFileUrl = new URL(url);
}catch (MalformedURLException e) {
e.printStackTrace();
}try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect(); InputStream is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
}catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
public class MyView extends View {
MyView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
// 首先定义一个paint
Paint paint = new Paint();
// 绘制矩形区域-实心矩形
// 设置颜色
paint.setColor(Color.BLUE);
// 设置样式-填充
paint.setStyle(Style.FILL);
// 绘制一个矩形
canvas.drawRect(new Rect(0, 0, getWidth(), getHeight()), paint);
// 绘空心矩形
// 设置颜色
paint.setColor(Color.RED);
// 设置样式-空心矩形
paint.setStyle(Style.STROKE);
canvas.drawRect(new Rect(10, 10, 100, 30), paint);
// 绘文字
// 设置颜色
paint.setColor(Color.GREEN);
// 绘文字
canvas.drawText("Hello", 10, 50, paint);
// 绘图
canvas.drawBitmap(bit, 10, 60, paint);
}
}
}