Android中实现输入图片地址浏览图片操作
来源:互联网 发布:平安车险计算器软件 编辑:程序博客网 时间:2024/05/22 05:33
其中这两个java文件 请参考 上一篇 文章
public class HttpUtils {public static String sendGet(String path) {String content = null;try {// 设置访问urlURL url = new URL(path);// 打开请求HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();// 设置请求的信息httpURLConnection.setRequestMethod("GET");// 设置请求是否超时时间httpURLConnection.setConnectTimeout(5000);// 判断服务器是否响应成功if (httpURLConnection.getResponseCode() == 200) {// 获取响应的输入流对象InputStream is = httpURLConnection.getInputStream();byte data[] = StreamTools.isToData(is);// 把转换成字符串content = new String(data);// 内容编码方式if (content.contains("gb2312")) {content = new String(data, "gb2312");}}// 断开连接httpURLConnection.disconnect();} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return content;}public static Bitmap sendGets(String path) {Bitmap bitmap = null;try {URL url = new URL(path);// 打开请求HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();// 设置请求的信息httpURLConnection.setRequestMethod("GET");// 设置请求是否超时时间httpURLConnection.setConnectTimeout(5000);// 判断服务器是否响应成功if (httpURLConnection.getResponseCode() == 200) {// 获取响应的输入流对象InputStream is = httpURLConnection.getInputStream();System.out.println("-----" + is);// 直接把这个is流转换成Bitmap对象bitmap = BitmapFactory.decodeStream(is);}// 断开连接httpURLConnection.disconnect();} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return bitmap;}}
实现其功能MainActivity类:
public class MainActivity extends Activity {// 声明控件public EditText et_url;public ImageView iv_ie;// 网络操作类public NetWorkUtils netWorkUtils;private Handler handler;public Bitmap bm;public static final int TEXT = 1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 获取et_url对象et_url = (EditText) findViewById(R.id.et_url);iv_ie = (ImageView) findViewById(R.id.iv_ie);// 实例化netWorkUtils = new NetWorkUtils(this);// 实例化这个处理者handler = new Handler() {public void handleMessage(Message msg) {super.handleMessage(msg);switch (msg.what) {case TEXT:iv_ie.setImageBitmap(bm);// 设置显示的文本break;default:break;}}};}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}public void sendHttp(View v){//设置网络boolean flag = netWorkUtils.setActiveNetWork();if(flag){//获取路径的值final String path = et_url.getText().toString();if(TextUtils.isEmpty(path)){Toast.makeText(MainActivity.this, "访问的url地址不能为空", Toast.LENGTH_LONG).show();}else{//子 线程new Thread(new Runnable() {@Overridepublic void run() {bm = HttpUtils.sendGets(path);handler.sendEmptyMessage(TEXT);}}).start();}}}}
==========================
布局:
<EditText android:id="@+id/et_url" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:ems="10" android:text="@string/url_text" > <requestFocus /> </EditText> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/et_url" android:onClick="sendHttp" android:text="@string/btn_text" /> <ImageView android:id="@+id/iv_ie" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button1" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_below="@+id/button1" android:src="@drawable/ic_launcher" />
String文件
<string name="url_text">http://172.22.64.20:8080/test/images/0.jpg</string>
<string name="btn_text">浏览</string>
记得添加访问网络的权限 ,请参考上一篇文章
- Android中实现输入图片地址浏览图片操作
- 实现输入图片地址浏览图片功能
- Android手机操作系统中实现图片浏览
- Android中实现地址栏输入网址能浏览该地址网页源码操作访问网络
- Android入门实例二 Android中图片浏览的实现
- Android实现图片预览浏览功能
- Android SmartImageView插件实现图片浏览功能
- Android实现对图片的浏览功能
- UIScrollView实现图片浏览
- 手风琴图片浏览实现
- 实现文件图片浏览功能,并把路径输入文本框
- Android之图片浏览
- Android WebView图片浏览
- android 图片浏览程序
- android在线浏览图片
- Android图片浏览
- android在线浏览图片
- Android ImageView 图片浏览
- 第十一次实验任务
- html5坦克大战
- 关于typedef的用法总结
- 第十一次实验任务
- vmware安装fedora 12出现问题
- Android中实现输入图片地址浏览图片操作
- SSL单向/双向认证详解
- 如何让你的C#写的更规范
- 文件操作几个简例
- 程序出错后 程序员给测试人员的20条高频回复
- RSA Bad Arguments
- 集锦
- 从海量数据中找出中位数
- GetEnumName应用