
将当作普通文件处理
File file = new File("/sdcard/demojpg");
FileInputStream fis = new FileInputStream(file);
int fileLen = fisavailable();
实现的功能为从服务器获取数据,在布局页面上显示。由于的个数是不确定的,因此采用在布局页面中定义多个ImageView来显示是不合理的。
(一)首先定义布局
android:id="@+id/id_layout_movie"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
(二)加载显示时获取到布局文件
RelativeLayout rl_Movie = (RelativeLayout) findViewById(Ridid_layout_movie);
(三)依次循环服务器获取的数据,一张一张设置显示的位置
//newWidth为显示的宽度,newHeight为显示的高度
RelativeLayoutLayoutParams lp1 = new RelativeLayoutLayoutParams( newWidth, newHeight);
设置lp1leftMargin和lp1topMargin的值
(四)最后设置rl_MovieaddView(iv, lp1)将加入布局文件中
这个方法用来将特定格式的压缩写入输出流(OutputStream)中,当然例如输出流与文件联系在一起,压缩后的也就是一个文件。如果压缩成功则返回true,其中有三个参数:
format是压缩后的的格式,可取值:BitmapCompressFormat JPEG、~PNG、~WEBP。
quality的取值范围为[0,100],值越小,经过压缩后失真越严重,当然文件也会越小。(PNG格式的会忽略这个值的设定)
stream指定压缩的输出的地方,比如某文件。
上述方法还有一个值得注意的地方是:当用BitmapFactory decode文件时可能返回一个跟原不同位深的,或者丢失了每个像素的透明值(alpha),比如说,JPEG格式的仅仅支持不透明的像素。文章android压缩在文末提到的下面这点可能就是这个原因:
当调用bitmapcompress(CompressFormatJPEG, 100, fos);保存为时发现背景为黑色,如下图:
这时只需要改成用png保存就可以了,bitmapcompress(CompressFormatPNG, 100, fos);,如下图:
下面是质量压缩的代码:
public static void compressBmpToFile(Bitmap bmp,File file){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int options = 80;//个人喜欢从80开始,
bmpcompress(BitmapCompressFormatJPEG, options, baos);
while (baostoByteArray()length / 1024 > 100) {
baosreset();
options -= 10;
bmpcompress(BitmapCompressFormatJPEG, options, baos);
}
try {
FileOutputStream fos = new FileOutputStream(file);
foswrite(baostoByteArray());
fosflush();
fosclose();
} catch (Exception e) {
eprintStackTrace();
}
}
这段代码来自Android压缩总结,我根据自己的需求改了改,但是大同小异,所以就直接贴了。
随着代码中的option逐渐变小,我们可以在logcat中打印baos的大小来查看的大小。我们也可以去掉while的循环条件,一直压缩下去看效果,最终一张照片可能就由原来的3、4M变成了几百K甚至几百B了。我在试的过程中将option设置成100,压缩后偶尔会出现一张3、4M的经过压缩后竟变成了6、7M,这里还是有点困惑,不知道为什么。
随后,我想把这个压缩后的(1、200KB)填充到ImageView中时却失败了,logcat中提示过大!这就是文章开头提到的问题,虽然我们通过质量压缩使File形式的文件缩小了,但是并没有改变的宽高,原先是10801920分辨率的经压缩后还是10801920,而File格式转换成Bitmap格式进入到内存中时,内存是根据的像素数量来给分配内存大小的,还是有好几M,因此填充ImageView失败。
顺便提一下,可以用bitmapgetByteCount()获取存储bitmap像素的内存大小,但是KITKAT(Android 44版本)以后用getAllocateByteCount()获取。一般情况下,后者返回值比前者大,比如,当bitmap被重用去decode另外更小的bitmaps时,或者被人为地配置一下属性值,比如setWidth()、setHeight()、reconfigure()时,如果bitmap不做以上 *** 作,二者的返回值应该是一样的。(译文,不太懂)
二、尺寸压缩
特点: 通过设置采样率, 减少的像素, 达到对内存中的Bitmap进行压缩
我们主要通过BitmapFactory中的decodeFile方法对进行尺寸压缩:
public static Bitmap decodeFile (String pathName, BitmapFactoryOptions opts)
public static Bitmap decodeFile (String pathName, BitmapFactoryOptions opts)
其中有两个参数:
pathName是文件的路径。
opts 就是所谓的采样率,它里边有很多属性可以设置,我们通过设置属性来达到根据自己的需要,压缩出指定的。其中比较常用的属性有:
boolean inJustDecodeBounds —— 如果设置为true,则只读取bitmap的宽高,而忽略内容。
int inSampleSize—— 如果>1,调用decodeFile方法后,就会得到一个更小的bitmap对象(已压缩)。比如设置为2,那么新Bitmap的宽高都会是原Bitmap宽高的1/2,总体大小自然就是原来的1/4了,以此类推。
boolean inPurgeable—— 如果设置为true,压缩后的像素占的内存将会在系统清理内存的时候被回收掉,当像素的信息再次被用到时将会自动重新decode该像素(比如getPixels()时)。(慎用!重复decode可以会造成UI的卡顿,API level 21 已弃用)
boolean inInputShareable—— 与inPurgeable配合使用,如果inPurgeable设置成false,自动忽略此值,如果inPurgeable为true,此值决定是否该bitmap能分享引用给输入数据(inputstream,array等),或者必须进行深拷贝。API level 21 已弃用。(这是译文,不太理解!!!)
下面是一段实现的代码
private Bitmap sizeCompres(String path, int rqsW, int rqsH) {
// 用option设置返回的bitmap对象的一些属性参数
final BitmapFactoryOptions options = new BitmapFactoryOptions();
optionsinJustDecodeBounds = true;// 设置仅读取Bitmap的宽高而不读取内容
BitmapFactorydecodeFile(path, options);// 获取到的宽高,放在option里边
final int height = optionsoutHeight;//的高度放在option里的outHeight属性中
final int width = optionsoutWidth;
int inSampleSize = 1;
if (rqsW == 0 || rqsH == 0) {
optionsinSampleSize = 1;
} else if (height > rqsH || width > rqsW) {
final int heightRatio = Mathround((float) height / (float) rqsH);
final int widthRatio = Mathround((float) width / (float) rqsW);
inSampleSize = heightRatio < widthRatio heightRatio : widthRatio;
optionsinSampleSize = inSampleSize;
}
return BitmapFactorydecodeFile(path, options);// 主要通过option里的inSampleSize对原进行按比例压缩
}
private Bitmap sizeCompres(String path, int rqsW, int rqsH) {
// 用option设置返回的bitmap对象的一些属性参数
final BitmapFactoryOptions options = new BitmapFactoryOptions();
optionsinJustDecodeBounds = true;// 设置仅读取Bitmap的宽高而不读取内容
BitmapFactorydecodeFile(path, options);// 获取到的宽高,放在option里边
final int height = optionsoutHeight;//的高度放在option里的outHeight属性中
final int width = optionsoutWidth;
int inSampleSize = 1;
if (rqsW == 0 || rqsH == 0) {
optionsinSampleSize = 1;
} else if (height > rqsH || width > rqsW) {
final int heightRatio = Mathround((float) height / (float) rqsH);
final int widthRatio = Mathround((float) width / (float) rqsW);
inSampleSize = heightRatio < widthRatio heightRatio : widthRatio;
optionsinSampleSize = inSampleSize;
}
return BitmapFactorydecodeFile(path, options);// 主要通过option里的inSampleSize对原进行按比例压缩
}
上面就是简单的质量压缩与尺寸压缩。
/
获取本地并指定高度和宽度
/
public static Bitmap getNativeImage(String imagePath)
{
BitmapFactoryOptions options = new BitmapFactoryOptions();
optionsinJustDecodeBounds = true;
// 获取这个的宽和高
Bitmap myBitmap = BitmapFactorydecodeFile(imagePath, options); //此时返回myBitmap为空
//计算缩放比
int be = (int)(optionsoutHeight / (float)200);
int ys = optionsoutHeight % 200;//求余数
float fe = ys / (float)200;
if (fe >= 05)
be = be + 1;
if (be <= 0)
be = 1;
optionsinSampleSize = be;
//重新读入,注意这次要把optionsinJustDecodeBounds 设为 false
optionsinJustDecodeBounds = false;
myBitmap = BitmapFactorydecodeFile(imagePath, options);
return myBitmap;
}
/
以最省内存的方式读取本地资源的 或者SDCard中的
@param imagePath
在SDCard中的路径
@return
/
public static Bitmap getSDCardImg(String imagePath)
{
BitmapFactoryOptions opt = new BitmapFactoryOptions();
optinPreferredConfig = BitmapConfigRGB_565;
optinPurgeable = true;
optinInputShareable = true;
//获取资源
return BitmapFactorydecodeFile(imagePath, opt);
}
takePictureIntentresolveActivity(getPackageManager()) != null
在官方文档中有描述:startActivityForResult()方法受到调用resolveActivity()的条件的保护,该方法返回可处理该意图的第一个活动组件,执行此检查很重要,因为如果您使用没有应用程序可以处理的意图调用startActivityForResult(),则您的应用程序将崩溃。所以只要结果不为空,就可以安全的使用意图,大概意思是检测手机中有没有相机。
另外一种检测相机的方法是
required=true 表示要安装该应用,手机必须有摄像头该硬件。要不然不允许安装
鸿洋的博客关于android70 以及 40 拍照封装的处理(点击跳转)
类似介绍比较好的文章推荐-Android 调用系统相机拍照攻略(已适配Android N)
以上就是关于Android程序开发如何获取图片的属性值,如图片的大小(KB)、上传或拍摄时间……全部的内容,包括:Android程序开发如何获取图片的属性值,如图片的大小(KB)、上传或拍摄时间……、android 存在数据库中的动态图片,如何读取出来,显示在ImageView中、android 图片质量压缩和尺寸压缩有什么区别等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)