package com.android.photomanager;
import android.app.Activity;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.MediaStore.Images.Media;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
/**
* 该类完成图片的检索,显示功能
* @author Administrator
*
*/
public class PhotoManager extends Activity {
public static final float DISPLAY_WIDTH = 200;
public static final float DISPLAY_HEIGHT = 200;
//这里采用ImageButton的原因是有Button的作用
private ImageButton photoView;
private TextView nameView;
private Cursor cursor;
private String photoPath; //存放某张图片对应的位置信息
private Bitmap currPhoto;
//这三个变量主要用来保存Media.DATA,Media.TITLE,Media.DISPLAY_NAME的索引号,来获取每列的数据
private int photoIndex;
//private int titleIndex;
private int nameIndex;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
photoView = (ImageButton)this.findViewById(R.id.image_view);
photoView.setOnClickListener(clickListener);
nameView = (TextView)this.findViewById(R.id.view_name);
//指定获取的列
String columns[] = new String[]{
Media.DATA,Media._ID,Media.TITLE,Media.DISPLAY_NAME
};
//cursor = this.managedQuery(Media.EXTERNAL_CONTENT_URI, columns, null, null, null);
cursor = this.getContentResolver().query(Media.EXTERNAL_CONTENT_URI, columns, null, null, null);
photoIndex = cursor.getColumnIndexOrThrow(Media.DATA);
//titleIndex = cursor.getColumnIndexOrThrow(Media.TITLE);
nameIndex = cursor.getColumnIndexOrThrow(Media.DISPLAY_NAME);
Log.v("HERE First:", "First Debug");
//显示第一张图片,但是首先要判断一下,Cursor是否有值
if(cursor.moveToFirst()){
showImage();
}
}
private View.OnClickListener clickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if(cursor.moveToNext()){
showImage();
}
}
};
/**
* 显示图像信息
*/
private void showImage(){
photoPath = cursor.getString(photoIndex); //这里获取到的就是图片存储的位置信息
//这里怎样获取图片呢?看decodeBitmap
Log.v("Photo Path:", photoPath);
currPhoto = decodeBitmap(photoPath);
photoView.setImageBitmap(currPhoto);
nameView.setText(cursor.getString(nameIndex));
}
/**
* 从path中获取图片信息
* @param path
* @return
*/
private Bitmap decodeBitmap(String path){
BitmapFactory.Options op = new BitmapFactory.Options();
op.inJustDecodeBounds = true;
Bitmap bmp = BitmapFactory.decodeFile(path, op); //获取尺寸信息
//获取比例大小
int wRatio = (int)Math.ceil(op.outWidth/DISPLAY_WIDTH);
int hRatio = (int)Math.ceil(op.outHeight/DISPLAY_HEIGHT);
//如果超出指定大小,则缩小相应的比例
if(wRatio > 1 && hRatio > 1){
if(wRatio > hRatio){
op.inSampleSize = wRatio;
}else{
op.inSampleSize = hRatio;
}
}
op.inJustDecodeBounds = false;
bmp = BitmapFactory.decodeFile(path, op);
return bmp;
}
}
Android多媒体学习之实现图像的编辑和合成
5星 · 超过95%的资源 需积分: 9 28 浏览量
2012-02-17
21:24:59
上传
评论 1
收藏 44KB RAR 举报
whdx666666
- 粉丝: 3
- 资源: 120
最新资源
- C#的Winform通过GDI+以及Datatable绘制基于CSV文件的风羽图
- 三菱PLC例程源码定型机超喂
- 3LN01C-TB-H-VB一款N-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- 13章Go微服务精讲:Go-Zero全流程实战即时通讯
- 3J14-VB一款P-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- 三菱PLC例程源码定时关闭所有的输出点定时停机365按x0复位
- 前端开发中国地图资源China.json
- 真实光伏电站数据集免费下载
- 3B14-VB一款N-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- 三菱PLC例程源码定时程序,程序带注解!!
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈