利用MediaStore检索图像+利用ExitInterface修改EXIF信息

来源:互联网 发布:淘宝店铺七天无理由 编辑:程序博客网 时间:2024/05/16 06:02

利用MediaStore检索图像

由于ContentProvider(当前为MediaStore)在应用程序之间是共享的,且大多数应用程序默认使用MediaStore,因此可以利用它来建立自己的图像库应用程序。

MediaStore和所有的内容提供器都以一种类似数据库的方式运作。从它们中选择记录,获得一个Cursor对象,并通过它来遍历结果。

1.首先创建一个打算返回列的字符串数组。对于MediaStore中的图像,其标准列在MediaStore.Images.Media类表示。

String[] columns={Media.DATA,Media._ID,Media.TITLE,Media.DISPLAY_NAME};

2.使用activity的managedQuery方法执行查询,第一个参数是Uri,然后是列名称的数组,后跟WHERE子句和WHERE子句的任何参数,最后是ORDER BY子句。

long oneHourAgo=System.currentTimeMillis()/1000-(60*60);String[] whereValues={""+oneHourAgo};cursor=managedQuery(Media.EXTERNAL_CONTENT_URI,columns,Media.DATE_ADDED+" > ?",whereValues,Media.DATE_ADDED+" ASC");

3. 返回的游标会告诉我们当前选择的每个列的索引

displayColumn=cursor.getColumnIndexOrThrow(Media.DISPLAY_NAME);
示例代码:
public class MediaStoreGallery extends Activity{public final static int DISPLAYWIDTH=200;public final static int DISPLAYHEIGHT=200;TextView titleTextView;ImageButton imageButton;Cursor cursor;Bitmap bmp;String imageFilePath;int fileColumn;int titleColumn;int displayColumn;@Overrideprotected void onCreate(Bundle savedInstanceState){// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.main);titleTextView=(TextView)this.findViewById(R.id.TitleTextView);imageButton=(ImageButton)this.findViewById(R.id.ImageButton);String[] columns={Media.DATA,Media._ID,Media.TITLE,Media.DISPLAY_NAME};cursor=managedQuery(Media.EXTERNAL_CONTENT_URI,columns,null,null,null);//long oneHourAgo=System.currentTimeMillis()/1000-(60*60);//String[] whereValues={""+oneHourAgo};//cursor=managedQuery(Media.EXTERNAL_CONTENT_URI,columns,//Media.DATE_ADDED+" > ?",whereValues,Media.DATE_ADDED+" ASC");fileColumn=cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);titleColumn=cursor.getColumnIndexOrThrow(Media.TITLE);//可简写成displayColumn=cursor.getColumnIndexOrThrow(Media.DISPLAY_NAME);if(cursor.moveToFirst()){titleTextView.setText(cursor.getString(titleColumn));imageFilePath=cursor.getString(fileColumn);bmp=getBitmap(imageFilePath);imageButton.setImageBitmap(bmp);}imageButton.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){// TODO Auto-generated method stubif(cursor.moveToNext()){titleTextView.setText(cursor.getString(titleColumn));imageFilePath=cursor.getString(fileColumn);bmp=getBitmap(imageFilePath);imageButton.setImageBitmap(bmp);}}});}private Bitmap getBitmap(String imageFilePath){//加载图像的尺寸而不是图像本身BitmapFactory.Options bmpFactoryOptions=new BitmapFactory.Options();bmpFactoryOptions.inJustDecodeBounds=true;Bitmap bmp=BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);int heightRatio=(int)Math.ceil(bmpFactoryOptions.outHeight/(float)dh);int widthRatio=(int)Math.ceil(bmpFactoryOptions.outWidth/(float)dw);Log.v("HEIGHT RATIO",""+heightRatio);Log.v("WIDTH RATIO",""+widthRatio);//如果两个比率都大于1,那么图像的一条边将大于屏幕if(heightRatio>1&&widthRatio>1){if(heightRatio>widthRatio){//如果高度比率更大,则根据它缩放bmpFactoryOptions.inSampleSize=heightRatio;}else{//若宽度比率更大,则根据它缩放bmpFactoryOptions.inSampleSize=widthRatio;}}//对它进行真正的解码bmpFactoryOptions.inJustDecodeBounds=false;bmp=BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);return bmp;}}

利用ExitInterface修改EXIF信息

1.EXIF 表示可交换的图像文件格式,它是在图像文件中保存元数据的一种标准方式。一般大多数标记与所捕获图像本身的数据相关,如ExposureTime和ShutterSpeedValue。

然而,有一些标记可以考虑填写或修改。

UserComment:由用户生成的备注

ImageDescription:标题

Artist:图像的创建者或接受者

Copyright:图像的版权持有人

Software:用于创建图像的软件

Android为我们提供了一种读写EXIF数据的方法,该方法的主要类是ExifInterface。

示例代码:

//如何使用ExifInterface从一个图像文件读取特定的EXIF数据ExifInterface ei=new ExifInterface(imageFilePath);String imageDescription=ei.getAttribute("ImageDescription");if(imageDescription!=null){Log.v("EXIF",imageDescription);}//如何使用ExifInterface将EXIF数据保存到图像文件中ei.setAttribute("ImageDescription","Something New");



原创粉丝点击