本文演示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. for循环的一种简化

    数组: var arr = [1, 2, 3, 5, 6]; 传统的教科书式的循环写法: for(var i=0; i<arr.length; i++){ console.log(arr[i]) ...

  2. Socket开发框架之数据加密及完整性检查

    在前面两篇介绍了Socket框架的设计思路以及数据传输方面的内容,整个框架的设计指导原则就是易于使用及安全性较好,可以用来从客户端到服务端的数据安全传输,那么实现这个目标就需要设计好消息的传输和数据加 ...

  3. Winform 显示Gif图片

    代码如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data ...

  4. ASP.NET Core Web API 开发-RESTful API实现

    ASP.NET Core Web API 开发-RESTful API实现 REST 介绍: 符合REST设计风格的Web API称为RESTful API. 具象状态传输(英文:Representa ...

  5. 做贴吧系统,偶然发现使用iframe的弊端

    个人拙见 常听人说起现在不建议使用iframe框架,而我却一再使用,也许是能力有限,一直没在意有什么弊端. 我这此使用的事左右iframe框架,并且只在主页中定义了大背景,每个页面并没有分别定义背景, ...

  6. Mssql中一些常用数据类型的说明和区别

    Mssql中一些常用数据类型的说明和区别 1.bigint 占用8个字节的存储空间,取值范围在-2^63 (-9,223,372,036,854,775,808) 到 2^63-1 (9,223,37 ...

  7. 你知道url中的特殊符号含义么

    1.# #代表网页中的一个位置.其右面的字符,就是该位置的标识符.比如,http://www.example.com/index.html#print就代表网页index.html的print位置.浏 ...

  8. No.017:Letter Combinations of a Phone Number

    问题: Given a digit string, return all possible letter combinations that the number could represent.A ...

  9. Verilog学习笔记设计和验证篇(五)...............层次化事件队列

    详细的了解层次化事件队列有助于理解Verilog的阻塞赋值和非阻塞赋值功能.所谓层次化事件队列指的是用于调度仿真时间的不同Verilog事件队列.在IEEE的5.3节中定义了层次化事件队列在逻辑上分为 ...

  10. Yii2.0学习笔记:创建登录表单

    第一步:在model层创建一个EntryForm.php文件 复制以下代码,注意model的文件.方法.类的命名规范(大小写) <?php namespace app\models; use Y ...