Android 获取SDCard上图片和视频的缩略图
获取图片缩略图和视频缩略图的方法:
Java代码:
import java.io.File;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ThumbnailUtils;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.widget.ImageView;
/**
* 获取图片和视频的缩略图
* 这两个方法必须在2.2及以上版本使用,因为其中使用了ThumbnailUtils这个类
*/
public class AndroidTestActivity extends Activity {
private ImageView imageThumbnail;
private ImageView videoThumbnail; /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); imageThumbnail = (ImageView) findViewById(R.id.image_thumbnail);
videoThumbnail = (ImageView) findViewById(R.id.video_thumbnail); String imagePath = Environment.getExternalStorageDirectory()
.getAbsolutePath()
+ File.separator
+ "photo"
+ File.separator
+ "yexuan.jpg"; String videoPath = Environment.getExternalStorageDirectory()
.getAbsolutePath()
+ File.separator
+ "video"
+ File.separator
+ "醋点灯.avi"; imageThumbnail.setImageBitmap(getImageThumbnail(imagePath, , ));
videoThumbnail.setImageBitmap(getVideoThumbnail(videoPath, , ,
MediaStore.Images.Thumbnails.MICRO_KIND));
} /**
* 根据指定的图像路径和大小来获取缩略图
* 此方法有两点好处:
* 1. 使用较小的内存空间,第一次获取的bitmap实际上为null,只是为了读取宽度和高度,
* 第二次读取的bitmap是根据比例压缩过的图像,第三次读取的bitmap是所要的缩略图。
* 2. 缩略图对于原图像来讲没有拉伸,这里使用了2.2版本的新工具ThumbnailUtils,使
* 用这个工具生成的图像不会被拉伸。
* @param imagePath 图像的路径
* @param width 指定输出图像的宽度
* @param height 指定输出图像的高度
* @return 生成的缩略图
*/
private Bitmap getImageThumbnail(String imagePath, int width, int height) {
Bitmap bitmap = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
// 获取这个图片的宽和高,注意此处的bitmap为null
bitmap = BitmapFactory.decodeFile(imagePath, options);
options.inJustDecodeBounds = false; // 设为 false
// 计算缩放比
int h = options.outHeight;
int w = options.outWidth;
int beWidth = w / width;
int beHeight = h / height;
int be = ;
if (beWidth < beHeight) {
be = beWidth;
} else {
be = beHeight;
}
if (be <= ) {
be = ;
}
options.inSampleSize = be;
// 重新读入图片,读取缩放后的bitmap,注意这次要把options.inJustDecodeBounds 设为 false
bitmap = BitmapFactory.decodeFile(imagePath, options);
// 利用ThumbnailUtils来创建缩略图,这里要指定要缩放哪个Bitmap对象
bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
return bitmap;
} /**
* 获取视频的缩略图
* 先通过ThumbnailUtils来创建一个视频的缩略图,然后再利用ThumbnailUtils来生成指定大小的缩略图。
* 如果想要的缩略图的宽和高都小于MICRO_KIND,则类型要使用MICRO_KIND作为kind的值,这样会节省内存。
* @param videoPath 视频的路径
* @param width 指定输出视频缩略图的宽度
* @param height 指定输出视频缩略图的高度度
* @param kind 参照MediaStore.Images.Thumbnails类中的常量MINI_KIND和MICRO_KIND。
* 其中,MINI_KIND: 512 x 384,MICRO_KIND: 96 x 96
* @return 指定大小的视频缩略图
*/
private Bitmap getVideoThumbnail(String videoPath, int width, int height,
int kind) {
Bitmap bitmap = null;
// 获取视频的缩略图
bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, kind);
System.out.println("w"+bitmap.getWidth());
System.out.println("h"+bitmap.getHeight());
bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
return bitmap;
} }
main.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="图片缩略图" /> <ImageView
android:id="@+id/image_thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="视频缩略图" /> <ImageView
android:id="@+id/video_thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </LinearLayout>
Android 获取SDCard上图片和视频的缩略图的更多相关文章
- iOS - 选取相册中iCloud云上图片和视频的处理
关于iOS选取相册中iCloud云上图片和视频 推荐看:TZImagePickerController的源码,这个是一个非常靠谱的相册选择图片视频的库 .当然也可以自己写 如下遇到的问题 工作原因, ...
- Android 获取SDCard中某个目录下图片
本文介绍Android开发中如何获取SDCard中某目录下的所有图片并显示出来,下面的我们提供的这个函数是通用的,只要提供路径就可以查询出该目录下所有图片的路径信息,并保存到一个List<Str ...
- android 如何获取手机的图片、视频、音乐
在android 开发中,很多时候,我们会需要调用到用户本机的照片.视频或者是音乐让用户选择,来进行我们APP对应的操作. button.setOnClickListener(new OnClickL ...
- Android获取ImageView上的图片,和一个有可能遇到的问题!
1.在获取图片前先调用setDrawingCacheEnabled(true)这个方法: 举例:mImageView.setDrawingCacheEnabled(true); 2.之后可以通过get ...
- Android获取网页上的图片的代码
public Bitmap getWebBitmap(String imgUrl) { Bitmap bitmap =null; try { InputStream inputStream = nul ...
- Android获取本地相册图片、拍照获取图片
需求:从本地相册找图片,或通过调用系统相机拍照得到图片. 容易出错的地方: 1,当我们指定了照片的uri路径,我们就不能通过data.getData();来获取uri,而应该直接拿到uri(用全局变量 ...
- android获取View上某点的颜色
//根据坐标获取 ImageView imageView = ((ImageView)v); Bitmap bitmap = ((BitmapDrawable)imageView.getDrawabl ...
- 【Android】读取sdcard上的图片
Android读取sdcard上的图片是很easy的事情,以下用一个样例来说明这个问题. 首先,在sdcard上有一张已经准备好的img25.jpg 以下,须要做的是把这张图片读取到app中显示. 做 ...
- Android之获取sdcard卡的信息
public static SDCardInfo getSDCardInfo() { String sDcString = android.os.Environment.getExternalStor ...
随机推荐
- js事件模型
连接在此 http://www.cnblogs.com/zqstc/archive/2009/11/26/1611464.html
- maven是什么?(转自oracle官网)
Maven 是一个项目管理和构建自动化工具.但是对于我们程序员来说,我们最关心的是它的项目构建功能.所以这里我们介绍的就是怎样用 maven 来满足我们项目的日常需要.Maven 使用惯例优于配置的原 ...
- Java输出日历
源码链接:http://pan.baidu.com/s/1o6xeybK
- linux/windows系统oracle数据库简单冷备同步
linux/windows系统oracle数据库简单冷备同步 我们有一个财务系统比较看重财务数据的安全性,同时我们拥有两套系统,一个生产环境(linux),一个应急备份环境(windows).备份环境 ...
- dbforge studio for mysql 怎样破解
下载好dbforge studio压缩包有两个exe,dbforge.studio.for.mysql.6.0.315-loader.exe ,和dbforgemysql.exe,安装后目录在C:\P ...
- Excel对话框大全
Excel对话框大全 序号 名称 描述 1 Application.Dialogs(1).Show 是调用打开对话框 2 Application.Dialogs(5或145).Show 是调用另存为 ...
- Hdu 4514 湫湫系列故事——设计风景线
湫湫系列故事--设计风景线 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total ...
- 线段树(单点更新)HDU1166、HDU1742
在上一篇博文里面,我提到了我不会线段树,现在就努力地学习啊! 今天AC一题感觉都很累,可能是状态不佳,在做HDU1166这题目时候,RE了无数次. 原因是:我的宏定义写错了,我已经不是第一犯这种错误了 ...
- HttpClient4.3 使用经验(一) 简单使用
package com.wp.nevel.base.utils; import java.io.BufferedOutputStream; import java.io.BufferedReader; ...
- 【实习记】2014-08-29算法学习Boyer-Moore和最长公共子串(LCS)
昨天的问题方案一:寻找hash函数,可行性极低.方案二:载入内存,维护成一个守护进程的服务.难度比较大.方案三:使用前5位来索引,由前3位增至前5位唯一性,理论上是分拆记录扩大100倍,但可以 ...