本文演示android中图片加载到内存

首先设计界面:

代码如下:

<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:orientation="vertical"
tools:context=".MainActivity" > <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="load"
android:text="加载图片到内存" /> <ImageView
android:id="@+id/iv"
android:layout_width="fill_parent"
android:layout_height="fill_parent" /> </LinearLayout>

往mnt/sdcard中上传测试图片

添加逻辑部分代码:

public class MainActivity extends Activity {

    private ImageView iv;

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv); } public void load() {
Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/a.jpg");
iv.setImageBitmap(bitmap); }
}

运行代码,产生错误,原因是图片太大(选取的是大照片)

可以使用BitmapFactory中包含的静态类Options在不解析图片信息的前提下得到图片的宽高信息:

    BitmapFactory.Options opts = new Options();
// 不去解析图片信息,只是得到图片的头部信息 宽高
opts.inJustDecodeBounds = true; BitmapFactory.decodeFile("/sdcard/a.jpg", opts);
int imageHeight = opts.outHeight;
int imagewidth = opts.outWidth;
System.out.println("图片宽:" + imagewidth);
System.out.println("图片高" + imageHeight);

运行一下:

09-04 06:09:10.519: I/System.out(1812): 图片宽:2560
09-04 06:09:10.519: I/System.out(1812): 图片高1920

接下来得到手机屏幕的宽高:

        //得到手机屏幕的宽高
WindowManager wm = (WindowManager)getSystemService(WINDOW_SERVICE);
int height = wm.getDefaultDisplay().getHeight();
int width = wm.getDefaultDisplay().getWidth();

可以看到getHeight与getWidth方法均已过时,使用下面的方法替代:

        // 得到手机屏幕的宽高
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
Point outSize = new Point();
wm.getDefaultDisplay().getSize(outSize); // 3.0以后的版本才能使用

完整代码如下:

package com.wuyudong.loadimage;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Point;
import android.view.Menu;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView; public class MainActivity extends Activity { private ImageView iv;
private int windowHeight;
private int windowWidth; protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv); // 得到手机屏幕的宽高
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
windowHeight = wm.getDefaultDisplay().getHeight();
windowWidth = wm.getDefaultDisplay().getWidth(); //Point outSize = new Point();
//wm.getDefaultDisplay().getSize(outSize); // 3.0以后的版本才能使用
//windowHeight = outSize.y;
//windowWidth = outSize.x;
} public void load(View view) {
// Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/a.jpg");
// iv.setImageBitmap(bitmap);
// 图片解析的配置
BitmapFactory.Options opts = new Options();
// 不去解析图片信息,只是得到图片的头部信息 宽高
opts.inJustDecodeBounds = true; BitmapFactory.decodeFile("/sdcard/a.jpg", opts);
int imageHeight = opts.outHeight;
int imagewidth = opts.outWidth;
System.out.println("图片宽:" + imagewidth);
System.out.println("图片高" + imageHeight); // 计算缩放比例
int scaleX = imagewidth / windowWidth;
int scaleY = imageHeight / windowHeight;
int scale = 1;
if (scaleX > scaleY & scaleY >= 1) {
scale = scaleX;
}
if (scaleY > scaleX & scaleX >= 1) {
scale = scaleY;
}
//解析图片
opts.inJustDecodeBounds = false; //采样率
opts.inSampleSize = scale;
Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/a.jpg", opts);
iv.setImageBitmap(bitmap); }
}

Android 加载大图片到内存的更多相关文章

  1. Android -- 加载大图片到内存,从gallery获取图片,获取图片exif信息

    1. 加载大图片到内存,从gallery获取图片 android默认的最大堆栈只有16M, 图片像素太高会导致内存不足的异常, 需要将图片等比例缩小到适合手机屏幕分辨率, 再加载. 从gallery ...

  2. Android学习笔记_51_转android 加载大图片防止内存溢出

    首先来还原一下堆内存溢出的错误.首先在SD卡上放一张照片,分辨率为(3776 X 2520),大小为3.88MB,是我自己用相机拍的一张照片.应用的布局很简单,一个Button一个ImageView, ...

  3. android加载大图片到内存

    1)演示效果: 1)代码演示: 布局代码: 权限配置:

  4. Android(java)学习笔记236:多媒体之加载大图片到内存(Bitmap API)

    1.Bitmap (API使用) android里面的bitmap中,一个像素点需要4个byte去表示,这是因为android表示颜色是" argb ":其中 a 表示是透明度,然 ...

  5. Android开发中如何解决加载大图片时内存溢出的问题

    Android开发中如何解决加载大图片时内存溢出的问题    在Android开发过程中,我们经常会遇到加载的图片过大导致内存溢出的问题,其实类似这样的问题已经屡见不鲜了,下面将一些好的解决方案分享给 ...

  6. Android(java)学习笔记179:多媒体之加载大图片到内存(Bitmap API)

    1. Bitmap (API使用) android里面的bitmap中,一个像素点需要4个byte去表示,这是因为android表示颜色是" argb ":其中 a 表示是透明度, ...

  7. 图片_ _Android有效解决加载大图片时内存溢出的问题 2

    Android有效解决加载大图片时内存溢出的问题 博客分类: Android Android游戏虚拟机算法JNI 尽量不要使用setImageBitmap或 setImageResource或 Bit ...

  8. Android有效解决加载大图片时内存溢出的问题

    首先,您需要了解一下,图片占用内存的计算方法,传送门:http://blog.csdn.net/scry5566/article/details/11568751 尽量不要使用setImageBitm ...

  9. Android加载大图片OOM异常解决

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

随机推荐

  1. JVM之类加载器上篇

    首先我们先看一个示例程序: package com.tfdd.test; /** * @desc 类加载校验 * @author chenqm * @date 2016年2月2日 */ class S ...

  2. PhotoKit框架介绍及使用

    PhotoKit 是一套比 AssetsLibrary 更新更完整也更高效的ios照片处理库,对资源的处理跟 AssetsLibrary 有很大的不同.下面简单介绍下PhotoKit的几个基本概念 P ...

  3. 兼容各浏览器的iframe - onlaod事件

    上次工作中,在使用 Iframe+FormSubmit进行无刷新提交时,如果后台返回的数据有延迟,或者浏览器对Iframe内容的更改过慢的话,会遇到onload响应在Iframe内容改变之前触发,这也 ...

  4. DirectShow .Net 实现视频

    DirectShow .Net 实现视频 .获取视频采集设备IBaseFilter接口对象的方法 //获取所有视频设备名称 public ArrayList GetVideoInputDevice() ...

  5. 【转】Key/Value之王Memcached初探:一、掀起Memcached的盖头来

    一.Memcached是何方神圣? 在数据驱动的Web开发中,经常要重复从数据库中取出相同的数据,这种重复极大的增加了数据库负载.缓存是解决这个问题的好办法.但是ASP.NET中的HttpRuntim ...

  6. T-SQL删除重复数据

    数据重复分为两种情况:一种是每个字段都相同的完全重复,第二种是部分字段重复的结果集.比如Name字段重复,而其他字段不一定重复或者重复可以忽略. 第一种情况比较容易解决,使用select distin ...

  7. PHP多种形式发送邮件

    1. 使用 mail() 函数 没什么好讲的,就是使用系统自带的smtp系统来发送,一般是使用sendmail来发.这个按照各个系统不同而定.使用参考手册. 2. 使用管道的形式 昨天刚测试成功,使用 ...

  8. FreeBSD应该装gnome3做桌面

    目前freebsd pkg包管理体系的repo源多了一些,速度快了很多. 仓库中目前的版本为3.14,安装gnome3很简单. pkg install xorg gnome3 echo "e ...

  9. [javaSE] 注解-JDK中的注解

    java中的常见注解 jdk自带注解:@Override 覆盖  @Deprecated 过期  @Suppvisewarnings 压制警告 package com.tsh.ano; public ...

  10. No.014:Longest Common Prefix

    问题: Write a function to find the longest common prefix string amongst an array of strings. 官方难度: Eas ...