Android显示在线图片
来源:互联网 发布:双十一淘宝营销策略 编辑:程序博客网 时间:2024/04/28 07:29
转自:http://blog.csdn.net/ameyume/article/details/6106286
- import java.io.ByteArrayOutputStream;
- import java.io.InputStream;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import android.app.Activity;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ImageView;
- import android.widget.Toast;
- public class AndroidTest extends Activity {
- private static final String URL = "http://avatar.csdn.net/3/2/4/2_ameyume.jpg";
- private EditText pathText;
- private ImageView imageView;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- pathText = (EditText) this.findViewById(R.id.path);
- pathText.setText(URL);
- imageView = (ImageView) this.findViewById(R.id.imageView);
- Button button = (Button) this.findViewById(R.id.button);
- button.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- String path = pathText.getText().toString();
- try {
- byte[] data = getImage(path);
- if(data!=null){
- Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);// bitmap
- imageView.setImageBitmap(bitmap);// display image
- }else{
- Toast.makeText(AndroidTest.this, "Image error!", 1).show();
- }
- } catch (Exception e) {
- Toast.makeText(AndroidTest.this,"Newwork error!", 1).show();
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Get image from newwork
- * @param path The path of image
- * @return
- * @throws Exception
- */
- public static byte[] getImage(String path) throws Exception{
- URL url = new URL(path);
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setConnectTimeout(5 * 1000);
- conn.setRequestMethod("GET");
- InputStream inStream = conn.getInputStream();
- if(conn.getResponseCode()==200){
- return readStream(inStream);
- }
- return null;
- }
- /**
- * Get data from stream
- * @param inStream
- * @return
- * @throws Exception
- */
- public static byte[] readStream(InputStream inStream) throws Exception{
- ByteArrayOutputStream outStream = new ByteArrayOutputStream();
- byte[] buffer = new byte[1024];
- int len = 0;
- while( (len=inStream.read(buffer)) != -1){
- outStream.write(buffer, 0, len);
- }
- outStream.close();
- inStream.close();
- return outStream.toByteArray();
- }
- }
manifest.xml中增加网络权限
- <uses-permission android:name="android.permission.INTERNET" />
- Android显示在线图片
- Android显示在线图片
- C#在线显示图片
- android在线浏览图片
- android在线浏览图片
- CuteEditor在线编辑器按钮图片显示问题
- ueditor 图片在线管理显示不了
- ueditor 图片在线管理显示不了
- 解决ueditor编辑器图片在线管理图片无法显示
- 解决ueditor编辑器图片在线管理图片无法显示
- Android ListView显示图片
- android 显示gif图片
- Android 网络图片显示
- Android 显示图片
- android 图片缩略图显示
- android显示gif图片
- Android显示GIF图片
- Android TextView显示图片
- LARGE_INTEGER类型 和 QueryPerformanceFrequency()
- java工具类系列 (三.ExceptionUtils)
- 设置多个Web园导致HttpContext.Current.Application、静态变量不能取值的问题
- VIM查找替换归纳总结
- Android:适配不同分辨率&hdpi、mdpi、ldpi&横屏竖屏
- Android显示在线图片
- Mongodb亿级数据量的性能测试
- -javaBean
- Android屏幕分辨率详解(VGA、HVGA、QVGA、WVGA、WQVGA)
- Android实现下载图片并保存到SD卡中
- android 应用开发揭秘,ndk使用过程总结
- C++内存分配秘籍------new, malloc, GlobalAlloc详解
- 实现 类似资源管理器功能
- mongodb c++ 起步