android网络图片查看器
2014-12-22 13:29
337 查看
可以先启动一个tomcat服务器,在网络上放一张图片
布局
工具类
布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/path"/> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/imagepath" android:text="@string/realpath" android:inputType="text"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button" android:text="@string/button" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/image" android:contentDescription="@string/description" /> </LinearLayout>mainactivity
package cn.wonders.image; import cn.wonders.service.ImageService; import android.support.v7.app.ActionBarActivity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends ActionBarActivity { private EditText et; private ImageView ev; private Bitmap bitmap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_main); // final EditText et = (EditText) this.findViewById(R.id.imagepath); et = (EditText) this.findViewById(R.id.imagepath); Button button = (Button) this.findViewById(R.id.button); // final ImageView ev = (ImageView) this.findViewById(R.id.image); ev = (ImageView) this.findViewById(R.id.image); button.setOnClickListener(new ButtonClickListener()); // button.setOnClickListener(new View.OnClickListener() { // // @Override // public void onClick(View v) { // // 使用匿名内部类,隐式调用外部变量,外部变量需要final修饰。 // String path = et.getText().toString(); // byte[] data; // try { // data = ImageService.getImage(path); // Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); // ev.setImageBitmap(bitmap); // } catch (Exception e) { // e.printStackTrace(); // Toast.makeText(getApplicationContext(), R.string.error, Toast.LENGTH_LONG).show(); // } // //生成位图对象 // //显示图片 // } // }); // if (savedInstanceState == null) { // getSupportFragmentManager().beginTransaction() // .add(R.id.container, new PlaceholderFragment()).commit(); // } } Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); ev.setImageBitmap(bitmap); } }; private final class ButtonClickListener implements View.OnClickListener{ public void onClick(View v) { // String path = et.getText().toString(); try{ new Thread(){ @Override public void run(){ //你要执行的方法 //执行完毕后给handler发送一个空消息 try{ String path = et.getText().toString(); byte[] data = ImageService.getImage(path); bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); handler.sendEmptyMessage(0); }catch (Exception e) { e.printStackTrace(); Toast.makeText(getApplicationContext(), R.string.error, Toast.LENGTH_LONG).show(); } } }.start(); // byte[] data = ImageService.getImage(path); // Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); // ev.setImageBitmap(bitmap);//显示图片 }catch (Exception e) { e.printStackTrace(); Toast.makeText(getApplicationContext(), R.string.error, Toast.LENGTH_LONG).show(); } } } }
工具类
package cn.wonders.utils; import java.io.ByteArrayOutputStream; import java.io.InputStream; public class StreamTool { public static byte[] read(InputStream input) throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while((len=input.read(buffer))!=-1) { baos.write(buffer, 0, len); } input.close(); return baos.toByteArray(); } }
相关文章推荐
- Android 网络图片查看显示的实现方法
- android 查看网络图片
- Android之使用HttpURLConnection类查看网络图片以及网络源码
- Android使用GridView异步加载(弱引用)网络图片(支持点击查看)
- Android 利用JS 实现对网络图片添加点击事件 查看大图
- android 通过网络图片路径查看图片
- android查看网络图片
- Androidx学习笔记(31)--- android-smart-image-view查看网络图片
- android查看网络图片
- Android 图片浏览(PhotoBrowse),网络图片,资源图片查看浏览
- 在android中显示网络图片及查看页面源代码
- Android查看网络图片
- Android查看网络图片例子
- Android之查看网络图片和网页HTML
- android基础--网络图片查看器
- Android网络:开发浏览器(六)——功能完善之查看图片属性
- Android中的网络操作-网络图片从查看
- Android开发之网络图片查看方法BitmapFactory.decodeStream()学习01
- Android 实验——实现查看网络图片功能
- Android入门之网络图片查看器