Android 按指定大小读取图片的实例

在Android开发中,我们经常遇到Android读取图片大小超过屏幕显示的图(一般只要显示一定规格的预览图即可),在图片特别多或者图片显示很频繁的时候要特别注意这个问题,下面介绍个按指定大小读取图像的方法。

实现原理:首先获取图片文件的图像高和宽,如果小于指定比例,则直接读取;如果超过比例则按指定比例压缩读取。

捕获OutOfMemoryError时注意点:后面返回的是null,不要马上从别的地方再读图片,包括R文件中的,不然依然会抛出这个异常,一般在初始化的时候缓存默认图片,然后显示缓存中的图片。

/** 获取图像的宽高**/

public static int[] getImageWH(String path) { int[] wh = {-1, -1}; if (path == null) { return wh; } File file = new File(path); if (file.exists() && !file.isDirectory()) { try { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; InputStream is = new FileInputStream(path); BitmapFactory.decodeStream(is, null, options); wh[0] = options.outWidth; wh[1] = options.outHeight; } catch (Exception e) { Log.w(TAG, "getImageWH Exception.", e); } } return wh; } public static Bitmap createBitmapByScale(String path, int scale) { Bitmap bm = null; try { //获取宽高 int[] wh = getImageWH(path); if (wh[0] == -1 || wh[1] == -1) { return null; } //读取图片 BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = Math.max(wh[0]/scale, wh[1]/scale); InputStream is = new FileInputStream(path); bm = BitmapFactory.decodeStream(is, null, options); } catch (Exception e) { Log.w(TAG, "createBitmapByScale Exception.", e); } catch (OutOfMemoryError e) { Log.w(TAG, "createBitmapByScale OutOfMemoryError.", e); //TODO: out of memory deal.. } return bm; }

以上就是解决Android 读取图片大小显示的问题,有需要的朋友可以参考下。

时间: 2024-08-04 03:55:05

Android 按指定大小读取图片的实例的相关文章

Android 按指定大小读取图片的实例_Android

在Android开发中,我们经常遇到Android读取图片大小超过屏幕显示的图(一般只要显示一定规格的预览图即可),在图片特别多或者图片显示很频繁的时候要特别注意这个问题,下面介绍个按指定大小读取图像的方法. 实现原理:首先获取图片文件的图像高和宽,如果小于指定比例,则直接读取:如果超过比例则按指定比例压缩读取. 捕获OutOfMemoryError时注意点:后面返回的是null,不要马上从别的地方再读图片,包括R文件中的,不然依然会抛出这个异常,一般在初始化的时候缓存默认图片,然后显示缓存中的

PHP动态生成指定大小随机图片的方法_php技巧

本文实例讲述了PHP动态生成指定大小随机图片的方法.分享给大家供大家参考,具体如下: <?php $image_width = 100; $image_height = 100; $image_str = ''; if (isset($_GET['w'])) { $image_width = intval($_GET['w']); } if (isset($_GET['h'])) { $image_height = intval($_GET['h']); } if (isset($_GET['s

Android利用Intent实现读取图片操作_Android

本文实例演示如何从图库(Gallery)中读取图像并用ImageView将它显示出来,供大家参考,具体内容如下 运行本示例前,需要先利用相机模拟拍摄一些图片到图库中. 1.运行截图    2.主要设计步骤 (1)添加ch1203_ReadGallery.axml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.androi

Android利用Intent实现读取图片操作

本文实例演示如何从图库(Gallery)中读取图像并用ImageView将它显示出来,供大家参考,具体内容如下 运行本示例前,需要先利用相机模拟拍摄一些图片到图库中. 1.运行截图 2.主要设计步骤 (1)添加ch1203_ReadGallery.axml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.c

Android实现返回拍摄的图片功能实例_Android

本文实例讲述了Android实现返回拍摄的图片功能.分享给大家供大家参考.具体如下: 第一步: try { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, 0); } catch (ActivityNotFoundException e) { // Do nothing for now } 第二步: @Override protected void onActi

Android截屏保存png图片的实例代码_Android

复制代码 代码如下: import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException; import android.app.Activity;import android.graphics.Bitmap;import android.graphics.Rect;import android.util.Log;import android.view.View; publ

Android截屏保存png图片的实例代码

复制代码 代码如下:import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException; import android.app.Activity;import android.graphics.Bitmap;import android.graphics.Rect;import android.util.Log;import android.view.View; publi

Android中截取当前屏幕图片的实例代码_Android

复制代码 代码如下: /**     * 获取和保存当前屏幕的截图     */    private void GetandSaveCurrentImage()      {          //1.构建Bitmap          WindowManager windowManager = getWindowManager();          Display display = windowManager.getDefaultDisplay();          int w = d

Android中截取当前屏幕图片的实例代码

复制代码 代码如下:/**     * 获取和保存当前屏幕的截图     */    private void GetandSaveCurrentImage()      {          //1.构建Bitmap          WindowManager windowManager = getWindowManager();          Display display = windowManager.getDefaultDisplay();          int w = di