我用tomcat部署了一个web项目,但是在android上却不能获取到图片

zrc 发布于 2013/12/13 17:54
阅读 876
收藏 0
package com.example.getimagefrominternet;


import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;


import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.ImageView;


public class MainActivity extends Activity {
private ImageView IV;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IV = (ImageView) findViewById(R.id.imageview);
new Thread(new Runnable() {


@Override
public void run() {
// TODO 自动生成的方法存根
final Bitmap bitmap = getImage("http://192.168.1.105:8080/test/images/a.png");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
IV.post(new Runnable() {


@Override
public void run() {
// TODO 自动生成的方法存根
IV.setImageBitmap(bitmap);
}
});
}
}).start();


}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}


public Bitmap getImage(String path) {
Bitmap bm = null;
try {
URL url = new URL(path);
URLConnection connection = url.openConnection();
connection.connect();
InputStream IS = connection.getInputStream();
bm = BitmapFactory.decodeStream(IS);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return bm;


}


@Override
protected void onDestroy() {
// TODO 自动生成的方法存根


if (Thread.currentThread() != null) {
Thread.currentThread().interrupted();


}
super.onDestroy();
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO 自动生成的方法存根
if (item.getItemId() == R.id.menu_settings) {
this.finish();
}
return true;
}


}

加载中
0
Glide
Glide
代码貌似没错,ip是你本地IP?试试10.0.0.1
zrc
zrc
我知道哪的问题了,是我在android上没有添加网络权限。哎。太晃了
matcloud
matcloud
10.0.0.2才对吧,端口还必须是8090
返回顶部
顶部