android如何从服务器取多张图片,在本地保存并显示
用BItMap类,处理下载的多张图片,并保存到本地一个目录,然后在android里边显示,哪位大哥有代码啊,分享下,急需,呵呵,谢谢!!!!!...
用BItMap类,处理下载的多张图片,并保存到本地一个目录,然后在android里边显示,哪位大哥有代码啊,分享下,急需,呵呵,谢谢!!!!!
展开
2013-07-30
展开全部
//获取connection conn = getURLConnection(url); is = conn.getInputStream(); //获取Bitmap的引用 Bitmap bitmap = BitmapFactory.decodeStream(is) //获取长度 int length = (int) conn.getContentLength(); if (length != -1) { byte[] imgData = new byte[length]; byte[] temp=new byte[512]; int readLen=0; int destPos=0; while((readLen=is.read(temp))>0){ System.arraycopy(temp, 0, imgData, destPos, readLen); destPos+=readLen; } bitmap=BitmapFactory.decodeByteArray(imgData, 0, imgData.length); }可能想得不是很完善,你可以参考参考。
2013-07-30
展开全部
URL url=new URL(uri); HttpURLConnection connection=(HttpURLConnection) url.openConnection(); connection.connect(); InputStream is=connection.getInputStream(); BufferedInputStream bi=new BufferedInputStream(is); BufferedReader br=new BufferedReader(new InputStreamReader(is, "utf-8")); bi.close(); is.close(); Bitmap bm=BitmapFactory.decodeStream(is);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
可以用Bitmap和Canvas自已拼接成一幅图。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
使用AsyncTask
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new MyTask().execute("http://www.baidu.com/img/bdlogo.png");//下载一个baidu的logo
}
//第一个参数的需要处理的数据,第二个参数是进度值,第三个参数是处理结果的类型
public class MyTask extends AsyncTask<String,Integer,Bitmap> {
@Override
protected void onPostExecute(Bitmap result) {
Log.d("", "onPostExecute");
updateImageView(result);
}
@Override
protected void onPreExecute() {
Log.d("", "onPreExecute");
}
@Override
protected Bitmap doInBackground(String... args) {
String website = args[0];
HttpURLConnection conn = null;
InputStream ins = null;
try {
URL url = new URL(website);
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setDoInput(true);
if (conn.getResponseCode() == 200) {
Log.d("", "get image is ok");
ins = conn.getInputStream();
return BitmapFactory.decodeStream(ins);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ins != null) {
try {
ins.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
}
public void updateImageView(Bitmap result) {
if (result != null) {
ImageView img = (ImageView) findViewById(R.id.img);
img.setImageBitmap(result);
}
}
}
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new MyTask().execute("http://www.baidu.com/img/bdlogo.png");//下载一个baidu的logo
}
//第一个参数的需要处理的数据,第二个参数是进度值,第三个参数是处理结果的类型
public class MyTask extends AsyncTask<String,Integer,Bitmap> {
@Override
protected void onPostExecute(Bitmap result) {
Log.d("", "onPostExecute");
updateImageView(result);
}
@Override
protected void onPreExecute() {
Log.d("", "onPreExecute");
}
@Override
protected Bitmap doInBackground(String... args) {
String website = args[0];
HttpURLConnection conn = null;
InputStream ins = null;
try {
URL url = new URL(website);
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setDoInput(true);
if (conn.getResponseCode() == 200) {
Log.d("", "get image is ok");
ins = conn.getInputStream();
return BitmapFactory.decodeStream(ins);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ins != null) {
try {
ins.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
}
public void updateImageView(Bitmap result) {
if (result != null) {
ImageView img = (ImageView) findViewById(R.id.img);
img.setImageBitmap(result);
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询