标签:
在做项目中,获取网络数据是一件很常见的事情,各类数据在服务器端或直接print,或封以实体类扔进List转换为json,由客户端使用HttpClient的execute()方法接收,以完成数据交互的过程。
对于图片,楼主采取利用url获取图片对象的方法来获取:
GetPicture.java
import android.graphics.Bitmap; import android.graphics.BitmapFactory; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.CoreConnectionPNames; import java.io.IOException; import java.io.InputStream; /** * 获取网络图片工具类 * pictureUrl:图片url,可网络地址,可服务器路径 */ public class GetPicture { private Bitmap picture; public Bitmap getNetPicture(final String pictureUrl){ new Thread(new Runnable() { @Override public void run() { Bitmap img = null; HttpClient client = null; HttpGet request = null; HttpResponse response = null; try { request = new HttpGet(pictureUrl); client = new DefaultHttpClient(); client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 1000 * 5); // 链接超时 client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,1000 * 5); // 读取超时 response = client.execute(request); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { InputStream is = response.getEntity().getContent(); img = BitmapFactory.decodeStream(is); showToast("网络图片:"+pictureUrl+"获取成功!"); } else { showToast("网络图片:"+pictureUrl+"获取失败!"); } } catch (ClientProtocolException e) { e.printStackTrace(); showToast(e.getMessage()); } catch (IOException e) { e.printStackTrace(); showToast(e.getMessage()); } picture = img; } }).start(); return picture; } private void showToast(String words) { System.out.println(words); } }如代码所示,通过访问url来获取图片对象,利用文件流来实现缓存到内存,最后return给需要的地方
使用方法:
//获取图片对象
String url ="图片url";
GetPicture getPicture = new GetPicture();
Bitmap picture = getPicture.getNetPicture(url);
MainActivity.java:
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.ImageView; import com.absolom.GetNetPicture.Tools.GetPicture; public class MainActivity extends AppCompatActivity { Button GetPicture1; Button GetPicture2; ImageView ImageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String url1 = "http://e.hiphotos.baidu.com/zhidao/pic/item/8cb1cb1349540923f12939199458d109b3de4910.jpg"; String url2 = "http://e.hiphotos.baidu.com/zhidao/pic/item/aec379310a55b31907d3ba3c41a98226cffc1754.jpg"; ImageView = (ImageView)findViewById(R.id.ImageView); GetPicture getPicture = new GetPicture(); GetPicture1 = (Button)findViewById(R.id.GetPicture1); GetPicture2 = (Button)findViewById(R.id.GetPicture2); GetPicture1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ImageView.setImageBitmap(getPicture.getNetPicture(url1)); } }); GetPicture2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ImageView.setImageBitmap(getPicture.getNetPicture(url2)); } }); } }
标签:
原文地址:http://blog.csdn.net/qq_14923661/article/details/51334595