2011年5月17日火曜日

AndroidStudyMemo=Sharing Images

Storing an image in the local application directory, as demonstrated, might work for some
applications; however, other applications might find it useful if the image goes in the
shared image library on the device.The ContentResolver can be used in conjunction
with the MediaStore object to push the image into the shared image library.The following
example demonstrates storing the still image taken by the camera as an image file
within the MediaStore content provider, using the same camera image callback:

public void onPictureTaken(byte[] data, Camera camera) {
Log.v("Still", "Image data received from camera");
try {
Bitmap bm = BitmapFactory.decodeByteArray(
data, 0, data.length);
String fileUrl = MediaStore.Images.Media.
insertImage(getContentResolver(), bm,
"Camera Still Image",
"Camera Pic Sample App Took");
if (fileUrl == null) {
Log.d("Still", "Image Insert failed");
return;
} else {
Uri picUri = Uri.parse(fileUrl);
sendBroadcast(new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
picUri));
}
} catch (Exception e) {
Log.e("Still", "Error writing file", e);
}
}

The image is turned into a Bitmap object, which is passed into the insertImage()
method.This method creates an entry in the shared image library. After the image is inserted,
we use the returned URL to create a Uri object representing the new image's location,
which we instruct the Media Scanner to pick up by broadcasting a specialized
intent.To determine if the scan completed successfully, you can make a call to the static
MediaScannerConnection.scanFile() method, and provide a
MediaScannerConnection.OnScanCompletedListener class implementation.
Now the image is available to all applications that use the MediaStore content
provider, such as the Pictures application.

0 件のコメント:

コメントを投稿