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. spss入门

    spss下载地址:http://www.ddooo.com/softdown/53379.htm

  2. TMainMenu - 隐藏与显示菜单

    //隐藏与显示菜单 Self.Menu := nil; {隐藏菜单} Self.Menu := MainMenu1; {显示菜单}

  3. matplotlib之scatter自动绘制散点

    # 使用matplotlib.pyplot.scatter绘制散点 import matplotlib.pyplot as plt from pylab import mpl # 设置默认字体,解决中 ...

  4. 学习笔记之csrf

    CBV 添加 csrf 第一种: 指定方法方面添加 装饰器 @memethod_decorator(xxx) 第二种 全部添加: 注意 在类名前: @method_decorator(xxx,name ...

  5. Django-website 程序案例系列-5 模态对话框实现提交数据

    html代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  6. Redundant Paths POJ - 3177(边—双连通分量)

    题意: 在图中加边 看最少能通过加多少条边把 图变成边—双连通分量 解析: 先做一次dfs,不同的连通分量的low是不同的  注意重边 缩点 统计度为1的点  那么需要加的边为(ret+1)/2 #i ...

  7. UVa - 12050

    A palindrome is a word, number, or phrase that reads the same forwards as backwards. For example,the ...

  8. AtCoder Grand Contest 018 E Sightseeing Plan

    题意: 给定三个矩形,选定三个点,答案加上第一个点出发经过第二个点在第三个点结束的方案数,只能往右或往下走. 折腾了我半个多下午的题. 设三个矩形为$A,B,C$一个思路是枚举$B$的那个点$s(x, ...

  9. [FJOI2017]矩阵填数——容斥

    参考:题解 P3813 [[FJOI2017]矩阵填数] 题目大意: 给定一个 h∗w 的矩阵,矩阵的行编号从上到下依次为 1...h ,列编号从左到右依次 1...w . 在这个矩阵中你需要在每个格 ...

  10. 初探react(一)

    我们学习react首先是要了解react是什么,以及他的特点. React 是一个用于构建用户界面的 JAVASCRIPT 库,起源于 Facebook 的内部项目,用来架设 Instagram 的网 ...