Android图像处理简介の图像存储和元数据


Android提供Content Provider来实现应用程序之间的数据共享,provider提供了标准的接口用于存储和检索多种类型的数据。图像 、音频和视频的标准content provider就是MediaStore。

1)获取图像的URI

要获得标准的图像存储路径,我们需要获得MediaStore的引用,而这是通过content resolver来实现的(因为使用Content resolver可以获取content provider,而MediaStore就是一个content provider)。

传递指定的URI给content resolver,可以得到对应的content provider,由于是新增一张图像,所以使用insert方法,相应的URI是android.provider.MediaStore.Images.Media类定义的常量EXTERNAL_CONTENT_URI。这个常量说明我们要将图像存储到主外部存储器中,通常就是SD卡;如果要将图像存储到设备内存中,则使用INTERNAL_CONTENT_URI。当然对于媒体文件的存储而言,由于尺寸一般都比较大,因此会优先考虑使用EXTERNAL_CONTENT_URI。

Content resolver类的insert函数返回值是URI类型:

[java]
  1. Uri imageFileUri = getContentResolver().insert(  
  2.                         Media.EXTERNAL_CONTENT_URI, new ContentValues());  
  3. // Start the Camera App   
  4. Intent it = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);  
  5. it.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri);  
  6. startActivityForResult(it, CAMERA_RESULT);  

上面代码中的ContentValues对象是捕获的图像在创建时要关联的元数据,当然,上面的元数据是空的。我们可以使用put函数将元数据信息写入ContentValues中,ContentValues是以键值对的形式存储数据的,键名是定义在android.provider.MediaStore.Images.Media类中的常量:

[java]
  1. // Save the name and description of an image in a ContentValues map   
  2. ContentValues contentValues = new ContentValues(3);  
  3. contentValues.put(Media.DISPLAY_NAME, "ASCE1885_TITLE");  
  4. contentValues.put(Media.DESCRIPTION, "ASCE1885_DESCRIPTION");  
  5. contentValues.put(Media.MIME_TYPE, "image/jpeg");  
  6.                   
  7. // Add a new recode without the bitmap, but with some values set.   
  8. // insert() returns the URI of the new record   
  9. Uri imageFileUri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, contentValues);  

上面获取的Uri可能类似于:

content://media/external/images/media/16

这里说明一点,以content开头的Uri一般都是被content provider使用的,例如上面的Uri是被MediaStore使用的一样。

反过来根据Uri,我们可以用来检索这个Uri对应路径中的图像数据,代码如下:

[java]
  1. Bitmap bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri),null,bmpFactory);  
  2. <p> </p>  

在我们捕获图像并存放在MediaStore中后,如果还想再增加元数据信息,那么可以使用ContentResolver的update函数来实现:

[java]
  1. // Update the MediaStore record with Title and Description   
  2. ContentValues contentValues = new ContentValues(3);  
  3. contentValues.put(Media.DISPLAY_NAME, "WEN1885_TITLE");  
  4. contentValues.put(Media.DESCRIPTION, "WEN1885_DESCRIPTION");  
  5. getContentResolver().update(imageFileUri, contentValues, nullnull);  

完整的代码例子如下,先看layout/main.xml文件:

[html]
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <ImageView  
  8.     android:id="@+id/ReturnedImageView"    
  9.     android:layout_width="wrap_content"   
  10.     android:layout_height="wrap_content"/>  
  11. <TextView   
  12.     android:layout_width="wrap_content"  
  13.     android:layout_height="wrap_content"  
  14.     android:text="Title:"  
  15.     android:id="@+id/TitleTextView" />  
  16. <EditText   
  17.     android:layout_width="fill_parent"  
  18.     android:layout_height="wrap_content"  
  19.     android:id="@+id/TitleEditText"/>  
  20. <TextView   
  21.     android:layout_width="wrap_content"  
  22.     android:layout_height="wrap_content"  
  23.     android:text="Description"  
  24.     android:id="@+id/DescriptionTextView"/>  
  25. <EditText   
  26.     android:layout_width="fill_parent"  
  27.     android:layout_height="wrap_content"  
  28.     android:id="@+id/DescriptionEditText"/>  
  29. <Button   
  30.     android:layout_width="wrap_content"  
  31.     android:layout_height="wrap_content"  
  32.     android:id="@+id/TakePictureButton"  
  33.     android:text="Take Picture"/>  
  34. <Button   
  35.     android:layout_width="wrap_content"  
  36.     android:layout_height="wrap_content"  
  37.     android:id="@+id/SaveDataButton"  
  38.     android:text="Save Data"/>  
  39. </LinearLayout>  
  • 1
  • 2
  • 3
  • 4
  • 下一页

相关内容