package mydemo.mycom.demo2;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast; import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL; public class ImagePrev extends ActionBarActivity implements View.OnClickListener { private ImageView iv;
private Button btn_prev_image;
private EditText et_image_path; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_prev); iv = (ImageView)findViewById(R.id.iv);
btn_prev_image = (Button)findViewById(R.id.btn_prev_image);
et_image_path = (EditText)findViewById(R.id.et_image_path);
btn_prev_image.setOnClickListener(this); } @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_image_prev, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
} @Override
public void onClick(View view) {
switch (view.getId())
{
case R.id.btn_prev_image:
String path = et_image_path.getText().toString().trim();
if(TextUtils.isEmpty(path))
{
Toast.makeText(this,"图片路径不能",Toast.LENGTH_SHORT).show();
return;
}
try
{
//浏览图片
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
int code = conn.getResponseCode();
if(code==200)
{
InputStream is = conn.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(is);
iv.setImageBitmap(bitmap);
}
else
{
Toast.makeText(this,"图片浏览失败",Toast.LENGTH_SHORT).show();
}
}catch (Exception e)
{
Toast.makeText(this,"图片浏览失败",Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
break;
}
}
}

Activity

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="mydemo.mycom.demo2.ImagePrev"> <ImageView
android:layout_weight="1000"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/iv"/>
<EditText
android:text="http://192.168.1.1:91/Resource/TopicInfo/2015-05-23/130768288778069472.png"
android:hint="图片路径"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/et_image_path"/> <Button
android:text="浏览"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_prev_image"/>
</LinearLayout>

android 加载图片的更多相关文章

  1. 图片--Android加载图片导致内存溢出(Out of Memory异常)

    Android在加载大背景图或者大量图片时,经常导致内存溢出(Out of Memory  Error),本文根据我处理这些问题的经历及其它开发者的经验,整理解决方案如下(部分代码及文字出处无法考证) ...

  2. Android加载图片OOM错误解决方式

    前几天做项目的时候,甲方要求是PAD (SAMSUNG P600 10.1寸 2560*1600)的PAD上显示高分辨率的大图片. SQLITE採用BOLD方式存储图片,这个存取过程就不说了哈,网上一 ...

  3. android 加载图片oom若干方案小结

    本文根据网上提供的一些技术方案加上自己实际开发中遇到的情况小结. 众所周知,每个Android应用程序在运行时都有一定的内存限制,限制大小一般为16MB或24MB(视手机而定).一般我们可以通过获取当 ...

  4. Android加载图片的策略

    实现图片缓存也不难,需要有相应的cache策略.这里我采用 内存-文件-网络 三层cache机制,其中内存缓存包括强引用缓存和软引用缓存(SoftReference),其实网络不算cache,这里姑且 ...

  5. android 加载图片框架--Glide使用详解

    一.简介 Glide,一个被google所推荐的图片加载库,作者是bumptech.这个库被广泛运用在google的开源项目中,包括2014年的google I/O大会上发布的官方app.(PS:众所 ...

  6. Android加载图片导致内存溢出(Out of Memory异常)

    Android在加载大背景图或者大量图片时,经常导致内存溢出(Out of Memory  Error),本文根据我处理这些问题的经历及其它开发者的经验,整理解决方案如下(部分代码及文字出处无法考证) ...

  7. android 加载图片防止内存溢出

    图片资源: private int fore[]; private int back[]; fore = new int[]{R.drawable.a0, R.drawable.a1, R.drawa ...

  8. 解决android加载图片时内存溢出问题

    尽量不要使用setImageBitmap或setImageResource或BitmapFactory.decodeResource来设置一张大图,因为这些函数在完成decode后,最终都是通过jav ...

  9. Android加载图片小结

    应用中用到图片加载需要解决的问题 无网络环境下图片不可用 图片的本地缓存,或者默认预加载的图片 低配置机型,加载图像资源超内存(OutOfMemory, OoM) 需要合理使用内存,尤其是bitmap ...

  10. android 加载图片圆角等功能的处理

    以Glide为例: Glide.with(getContext()).load(item.getSoftLogo()).transform(this.glideRoundTransform).into ...

随机推荐

  1. 修改maven的默认jdk版本

    问题: 创建maven项目的时候,jdk版本默认使用的是1.5版本. 解决: 1.修改maven的settings.xml文件,添加如下: <profile> <id>jdk- ...

  2. Aladdin and the Flying Carpet LightOJ - 1341 (素数打表 + 算术基本定理)

    题意: 就是求a的因数中大于b的有几对 解析: 先把素数打表 运用算术基本定理 求出a的所有因数的个数 然后减去小于b的因数的个数 代码如下: #include <iostream> #i ...

  3. day22 ramdom 模块

    import random #随机整数 random.randint(1,5) # 大于等于1且小于等于5之间的整数 random.randrange(1,10,2) # 大于等于1且小于10之间的奇 ...

  4. Deep Learning(深度学习)学习笔记整理系列二

    声明: 1)该Deep Learning的学习系列是整理自网上很大牛和机器学习专家所无私奉献的资料的.具体引用的资料请看参考文献.具体的版本声明也参考原文献. 2)本文仅供学术交流,非商用.所以每一部 ...

  5. sharepoint my site setting

    参考这个guide : http://technet.microsoft.com/en-us/library/ee624362.aspx User profile service 不能打开, 原因是s ...

  6. 软Raid50制作

    =====创建Raid50的步骤====== 1.创建分区[root@localhost ~]# fdisk /dev/sdb[root@localhost ~]# fdisk /dev/sdc[ro ...

  7. Java -- JDBC 学习--获取数据库链接

    数据持久化 持久化(persistence): 把数据保存到可掉电式存储设备中以供之后使用.大多数情况下,特别是企业级应用,数据持久化意味着将内存中的数据保存到硬盘上加以”固化”,而持久化的实现过程大 ...

  8. Libre 6012 「网络流 24 题」分配问题 (网络流,费用流)

    Libre 6012 「网络流 24 题」分配问题 (网络流,费用流) Description 有n件工作要分配给n个人做.第i个人做第j件工作产生的效益为\(c_{ij}\).试设计一个将n件工作分 ...

  9. Linux command ------ vi / vim

    EDIT mode to GENERAL mode: press ESC General mode: operate file :q!    :force to close the file but ...

  10. SQL Server 2008 数据库回滚到某个时间点

    数据库回滚到时间的的前提: 事务日志完整,数据库在完整恢复模式下进行过一次完整备份,数据库没有进行过还原操作(惨痛教训). 当数据库误操作时,切记冷静,不然问题就是滚雪球, 在不做下一步错误前  可观 ...