android 加载图片
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 加载图片的更多相关文章
- 图片--Android加载图片导致内存溢出(Out of Memory异常)
Android在加载大背景图或者大量图片时,经常导致内存溢出(Out of Memory Error),本文根据我处理这些问题的经历及其它开发者的经验,整理解决方案如下(部分代码及文字出处无法考证) ...
- Android加载图片OOM错误解决方式
前几天做项目的时候,甲方要求是PAD (SAMSUNG P600 10.1寸 2560*1600)的PAD上显示高分辨率的大图片. SQLITE採用BOLD方式存储图片,这个存取过程就不说了哈,网上一 ...
- android 加载图片oom若干方案小结
本文根据网上提供的一些技术方案加上自己实际开发中遇到的情况小结. 众所周知,每个Android应用程序在运行时都有一定的内存限制,限制大小一般为16MB或24MB(视手机而定).一般我们可以通过获取当 ...
- Android加载图片的策略
实现图片缓存也不难,需要有相应的cache策略.这里我采用 内存-文件-网络 三层cache机制,其中内存缓存包括强引用缓存和软引用缓存(SoftReference),其实网络不算cache,这里姑且 ...
- android 加载图片框架--Glide使用详解
一.简介 Glide,一个被google所推荐的图片加载库,作者是bumptech.这个库被广泛运用在google的开源项目中,包括2014年的google I/O大会上发布的官方app.(PS:众所 ...
- Android加载图片导致内存溢出(Out of Memory异常)
Android在加载大背景图或者大量图片时,经常导致内存溢出(Out of Memory Error),本文根据我处理这些问题的经历及其它开发者的经验,整理解决方案如下(部分代码及文字出处无法考证) ...
- android 加载图片防止内存溢出
图片资源: private int fore[]; private int back[]; fore = new int[]{R.drawable.a0, R.drawable.a1, R.drawa ...
- 解决android加载图片时内存溢出问题
尽量不要使用setImageBitmap或setImageResource或BitmapFactory.decodeResource来设置一张大图,因为这些函数在完成decode后,最终都是通过jav ...
- Android加载图片小结
应用中用到图片加载需要解决的问题 无网络环境下图片不可用 图片的本地缓存,或者默认预加载的图片 低配置机型,加载图像资源超内存(OutOfMemory, OoM) 需要合理使用内存,尤其是bitmap ...
- android 加载图片圆角等功能的处理
以Glide为例: Glide.with(getContext()).load(item.getSoftLogo()).transform(this.glideRoundTransform).into ...
随机推荐
- python之tkinter使用-多选框实现开关操作
# tkinter的Checkbutton实现开关操作 import tkinter as tk root = tk.Tk() root.title('开关') root.geometry('170x ...
- 一本通1587【例 3】Windy 数
1587: [例 3]Windy 数 时间限制: 1000 ms 内存限制: 524288 KB 题目描述 原题来自:SCOI 2009 Windy 定义了一种 Windy 数:不含前 ...
- LOJ2540 [PKUWC2018] 随机算法 【状压DP】
题目分析: 听说这题考场上能被$ O(4^n) $的暴力水过,难不成出题人是毕姥爷? 首先思考一个显而易见的$ O(n^2*2^n) $的暴力DP.一般的DP都是考虑最近的加入了哪个点,然后删除后递归 ...
- python3.5 opencv3显示视频fps
由于要进行多路视频的处理,所以fps就很重要 fps介绍 模板: 1.获取某一时刻的fps import time while True: start_time = time.time() # sta ...
- 在Android中通过Intent使用Bundle传递对象
IntentBundle传递对象SerializableParcelable Android开发中有时需要在应用中或进程间传递对象,下面详细介绍Intent使用Bundle传递对象的方法.被传递的对象 ...
- SQL Server 查
注:where语句是条件,后面加and或者or 时间日期:比时间需要时间加引号 模糊查询:where语句后面加like '%包含此关键字%'或者'以此关键字开头%'或者'%结尾' 排序查询:列名 o ...
- RMQ求解->ST表
ST表 这是一种神奇的数据结构,用nlogn的空间与nlongn的预处理得出O(1)的区间最大最小值(无修) 那么来看看这个核心数组:ST[][] ST[i][j]表示从i到i+(1<<j ...
- VB: 再次使用的体会
放下VB已经有7.8年的时候了. 记得在上学的时候,一直迷恋着它,学了三年的VB,写了不少小软件. 到了工作之后,转到JAVA后,就一直没用VB. 这次的项目由于与系统的相关性高以及安装文件的大小有限 ...
- Mysq中的流程控制语句的用法
这篇博客主要是总结一下Mysq中的流程控制语句的用法,主要是:CASE,IF,IFNULL,NULLIF 1.case CASE value WHEN [compare-value] THEN res ...
- MyEclipse中引用的maven配置文件只访问私服的配置
MyEclipse中要用到集成的maven,公司内网有个私服,办公机不能上外网. 这时Eclipse中设置引用的外部Setting配置文件中只需如下配置即可: 1.配置本地主机的maven仓库路径 & ...