Android图像处理简介の图像存储和元数据
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]- Uri imageFileUri = getContentResolver().insert(
- Media.EXTERNAL_CONTENT_URI, new ContentValues());
- // Start the Camera App
- Intent it = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
- it.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri);
- startActivityForResult(it, CAMERA_RESULT);
上面代码中的ContentValues对象是捕获的图像在创建时要关联的元数据,当然,上面的元数据是空的。我们可以使用put函数将元数据信息写入ContentValues中,ContentValues是以键值对的形式存储数据的,键名是定义在android.provider.MediaStore.Images.Media类中的常量:
[java]- // Save the name and description of an image in a ContentValues map
- ContentValues contentValues = new ContentValues(3);
- contentValues.put(Media.DISPLAY_NAME, "ASCE1885_TITLE");
- contentValues.put(Media.DESCRIPTION, "ASCE1885_DESCRIPTION");
- contentValues.put(Media.MIME_TYPE, "image/jpeg");
- // Add a new recode without the bitmap, but with some values set.
- // insert() returns the URI of the new record
- 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]- Bitmap bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri),null,bmpFactory);
- <p> </p>
在我们捕获图像并存放在MediaStore中后,如果还想再增加元数据信息,那么可以使用ContentResolver的update函数来实现:
[java]- // Update the MediaStore record with Title and Description
- ContentValues contentValues = new ContentValues(3);
- contentValues.put(Media.DISPLAY_NAME, "WEN1885_TITLE");
- contentValues.put(Media.DESCRIPTION, "WEN1885_DESCRIPTION");
- getContentResolver().update(imageFileUri, contentValues, null, null);
完整的代码例子如下,先看layout/main.xml文件:
[html]- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <ImageView
- android:id="@+id/ReturnedImageView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Title:"
- android:id="@+id/TitleTextView" />
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/TitleEditText"/>
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Description"
- android:id="@+id/DescriptionTextView"/>
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/DescriptionEditText"/>
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/TakePictureButton"
- android:text="Take Picture"/>
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/SaveDataButton"
- android:text="Save Data"/>
- </LinearLayout>
|
评论暂时关闭