CHAPTER1 Camera (MediaStore) EXIF 1
2 CHAPTER 1 SDK (intent) Camera Camera Camera Android Manifest xml Camera Camera <intent-filter> <action android:name= android.media.action.image_capture /> <category android:name= android.intent.category.default /> </intent-filter> Camera
3 Intent i = new Intent( android.media.action.image_capture ); MediaStore ACTION_IMAGE_CAPTURE Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); startactivity(i); Camera 1 1 Camera
4 CHAPTER 1 Camera startactivity startactivityforresult startactivityforresult Camera package com.apress.proandroidmedia.ch1.cameraintent; import android.app.activity; import android.content.intent; import android.graphics.bitmap; import android.os.bundle; import android.widget.imageview; public class CameraIntent extends Activity final static int CAMERA_RESULT = 0; ImageView imv; @Override public void oncreate(bundle savedinstancestate) super.oncreate(savedinstancestate); setcontentview(r.layout.main); Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); startactivityforresult(i, CAMERA_RESULT); protected void onactivityresult(int requestcode, int resultcode, Intent intent) super.onactivityresult(requestcode, resultcode, intent); if (resultcode == RESULT_OK) Bundle extras = intent.getextras(); Bitmap bmp = (Bitmap) extras.get( data );
5 imv = (ImageView) findviewbyid(r.id.returnedimageview); imv.setimagebitmap(bmp); layout/main xml <?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 ></ImageView> </LinearLayout> AndroidManifest xml <?xml version= 1.0 encoding= utf-8?> <manifest xmlns:android= http://schemas.android.com/apk/res/android android:versioncode= 1 android:versionname= 1.0 package= com.apress.proandroidmedia.ch1.cameraintent > <application android:icon= @drawable/icon android:label= @string/app_name > <activity android:name=.cameraintent android:label= @string/app_name > <intent-filter> <action android:name= android.intent.action.main /> <category android:name= android.intent.category.launcher /> </intent-filter> </activity> </application> <uses-sdk android:minsdkversion= 4 /> </manifest>
6 CHAPTER 1 extra( ) Camera onactivityresult extra data Bitmap (generic) // extra Bundle extras = intent.getextras(); // extra Bitmap bmp = (Bitmap) extras.get( data ); XML (layout/main xml) ImageView ImageView View ImageView id ReturnedImageView ImageView Activity findviewbyid XML setcontentview id ImageView XML <ImageView android:id= @+id/returnedimageview android:layout_width= wrap_content ë android:layout_height= wrap_content ></ImageView> ImageView Camera Bitmap imv = (ImageView) findviewbyid(r.id.returnedimageview);imv.setimagebitmap(bmp); ( 121 162 ) Camera
7 Camera 1 2 1 5 Camera extra extra MediaStore EXTRA_OUTPUT (extra ) Camera URI
8 CHAPTER 1 Camera SD myfavoritepicture jpg String imagefilepath = Environment.getExternalStorageDirectory().getAbsolutePath() ë + /myfavoritepicture.jpg ; File imagefile = new File(imageFilePath); Uri imagefileuri = Uri.fromFile(imageFile); Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); i.putextra(android.provider.mediastore.extra_output, imagefileuri); startactivityforresult(i, CAMERA_RESULT); Note imagefileuri = Uri.parse( file:///sdcard/myfavoritepicture.jpg ); HTC G1( ) 320 2048x1536 32 13MB BitmapFactory BitmapFactory BitmapFactory Options Bitmap
9 BitmapFactory BitmapFactory Options insamplesize Bitmap insamplesize 8 8 1 BitmapFactory.Options bmpfactoryoptions = new BitmapFactory.Options(); bmpfactoryoptions.insamplesize = 8; Bitmap bmp = BitmapFactory.decodeFile(imageFilePath, bmpfactoryoptions); imv.setimagebitmap(bmp); 100 Display currentdisplay = getwindowmanager().getdefaultdisplay(); int dw = currentdisplay.getwidth(); int dh = currentdisplay.getheight(); BitmapFactory Options injustdecodebounds true BitmapFactory BitmapFactory Options BitmapFactory BitmapFactory Options outheight BitmapFactory Options outwidth
10 CHAPTER 1 //. 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( HEIGHTRATIO, +heightratio); Log.v( WIDTHRATIO, +widthratio); ( ) ( ) BitmapFactory Options insamplesize //,. if (heightratio > 1 && widthratio > 1) if (heightratio > widthratio) //. bmpfactoryoptions.insamplesize = heightratio; else //. bmpfactoryoptions.insamplesize = widthratio; //. bmpfactoryoptions.injustdecodebounds = false; bmp = BitmapFactory.decodeFile(imageFilePath, bmpfactoryoptions);
11 1 3 package com.apress.proandroidmedia.ch1.sizedcameraintent; import java.io.file; import android.app.activity; import android.content.intent; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.net.uri; import android.os.bundle; import android.os.environment; import android.util.log; import android.view.display; import android.widget.imageview; public class SizedCameraIntent extends Activity final static int CAMERA_RESULT = 0; ImageView imv; String imagefilepath; @Override public void oncreate(bundle savedinstancestate) super.oncreate(savedinstancestate); setcontentview(r.layout.main); imagefilepath = Environment.getExternalStorageDirectory().getAbsolutePath() + /myfavoritepicture.jpg ; File imagefile = new File(imageFilePath); Uri imagefileuri = Uri.fromFile(imageFile); Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); i.putextra(android.provider.mediastore.extra_output, imagefileuri); startactivityforresult(i, CAMERA_RESULT); protected void onactivityresult(int requestcode, int resultcode, Intent intent)
12 CHAPTER 1 super.onactivityresult(requestcode, resultcode, intent); if (resultcode == RESULT_OK) // ImageView. imv = (ImageView) findviewbyid(r.id.returnedimageview); Display currentdisplay = getwindowmanager().getdefaultdisplay(); int dw = currentdisplay.getwidth(); int dh = currentdisplay.getheight(); //. 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( HEIGHTRATIO, + heightratio); Log.v( WIDTHRATIO, + 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); //. imv.setimagebitmap(bmp);
13 layout/main xml <?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 ></ImageView> </LinearLayout>
14 CHAPTER 1 ( ) MediaStore MediaStore MediaStore SizedCameraIntent MediaStore SD MediaStore MediaStore URI MediaStore insert URI android provider MediaStore Images Media EXTERNAL_CONTENT_ URI SD INTERNAL_ CONTENT_URI EXTERNAL_CONTENT_URI
15 (insert) URI URI CameraActivity Camera extra Uri imagefileuri = getcontentresolver().insert( Media.EXTERNAL_CONTENT_URI, new ContentValues()); Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); i.putextra(android.provider.mediastore.extra_output, imagefileuri); startactivityforresult(i, CAMERA_RESULT); ContentValues ContentValues ContentValues put ContentValues android provider MediaStore Images Media ( android provider MediaStore MediaColumns Media ) //. ContentValues contentvalues = new ContentValues(3); contentvalues.put(media.display_name, This is a test title ); contentvalues.put(media.description, This is a test description ); contentvalues.put(media.mime_type, image/jpeg ); //. // insert() URI. Uri imagefileuri = getcontentresolver().insert(media.external_content_uri, contentvalues); URI Camera
16 CHAPTER 1 URI Log content://media/external/images/media/16 URL http content URI content ( MediaStore) URI BitmapFactory InputStream BitmapFactory Bitmap bmp = BitmapFactory.decodeStream( ë getcontentresolver().openinputstream(imagefileuri), null, bmpfactoryoptions); MediaStore update update insert update URI //. ContentValues contentvalues = new ContentValues(3); contentvalues.put(media.display_name, This is a test title ); contentvalues.put(media.description, This is a test description ); getcontentresolver().update(imagefileuri,contentvalues,null,null);
17 MediaStore UI package com.apress.proandroidmedia.ch1.mediastorecameraintent; import java.io.filenotfoundexception; import android.app.activity; import android.content.intent; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.net.uri; import android.os.bundle; import android.util.log; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.imageview; import android.widget.textview; import android.widget.toast; import android.provider.mediastore.images.media; import android.content.contentvalues; public class MediaStoreCameraIntent extends Activity final static int CAMERA_RESULT = 0; Uri imagefileuri; // res/layout/main.xml ImageView returnedimageview; Button takepicturebutton; Button savedatabutton; TextView titletextview;
18 CHAPTER 1 TextView descriptiontextview; EditText titleedittext; EditText descriptionedittext; layout/main xml @Override public void oncreate(bundle savedinstancestate) super.oncreate(savedinstancestate); // res/layout/main.xml. setcontentview(r.layout.main); // UI. returnedimageview = (ImageView) findviewbyid(r.id.returnedimageview); takepicturebutton = (Button) findviewbyid(r.id.takepicturebutton); savedatabutton = (Button) findviewbyid(r.id.savedatabutton); titletextview = (TextView) findviewbyid(r.id.titletextview); descriptiontextview = (TextView) findviewbyid(r.id.descriptiontextview); titleedittext = (EditText) findviewbyid(r.id.titleedittext); descriptionedittext = (EditText) findviewbyid(r.id.descriptionedittext); oncreate setcontentview findviewbyid // takepicturebutton. // View.GONE. returnedimageview.setvisibility(view.gone); savedatabutton.setvisibility(view.gone); titletextview.setvisibility(view.gone); descriptiontextview.setvisibility(view.gone); titleedittext.setvisibility(view.gone); descriptionedittext.setvisibility(view.gone);
19 View GONE setvisibility View INVISIBLE // takepicturebutton takepicturebutton.setonclicklistener(new OnClickListener() public void onclick(view v) //. // URI. imagefileuri = getcontentresolver().insert(media.external_content_uri, ë new ContentValues()); ); // Camera. Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); i.putextra(android.provider.mediastore.extra_output, imagefileuri); startactivityforresult(i, CAMERA_RESULT); takepicturebutton OnClickListener startactivityforresult oncreate savedatabutton.setonclicklistener(new OnClickListener() public void onclick(view v) // MediaStore. ContentValues contentvalues = new ContentValues(3); contentvalues.put(media.display_name, titleedittext.gettext().tostring()); contentvalues.put(media.description, descriptionedittext.gettext().tostring()); getcontentresolver().update(imagefileuri,contentvalues,null,null); //. Toast bread = Toast.makeText(MediaStoreCameraIntent.this, Record ë Updated, Toast.LENGTH_SHORT);
20 CHAPTER 1 bread.show(); // takepicturebutton. // UI. takepicturebutton.setvisibility(view.visible); ); returnedimageview.setvisibility(view.gone); savedatabutton.setvisibility(view.gone); titletextview.setvisibility(view.gone); descriptiontextview.setvisibility(view.gone); titleedittext.setvisibility(view.gone); descriptionedittext.setvisibility(view.gone); savedatabutton OnClickListener Camera EditText ContentValues MediaStore protected void onactivityresult(int requestcode, int resultcode, Intent intent) super.onactivityresult(requestcode, resultcode, intent); if (resultcode == RESULT_OK) // Camera. // takepicturebutton. takepicturebutton.setvisibility(view.gone); // UI. savedatabutton.setvisibility(view.visible); returnedimageview.setvisibility(view.visible); titletextview.setvisibility(view.visible); descriptiontextview.setvisibility(view.visible); titleedittext.setvisibility(view.visible);
21 descriptionedittext.setvisibility(view.visible); //. int dw = 200; // 200 int dh = 200; // 200 try //. BitmapFactory.Options bmpfactoryoptions = new BitmapFactory.Options(); bmpfactoryoptions.injustdecodebounds = true; Bitmap bmp = BitmapFactory.decodeStream(getContentResolver(). ë openinputstream(imagefileuri), null, bmpfactoryoptions); int heightratio = (int)math.ceil(bmpfactoryoptions.outheight/(float)dh); int widthratio = (int)math.ceil(bmpfactoryoptions.outwidth/(float)dw); Log.v( HEIGHTRATIO, +heightratio); Log.v( WIDTHRATIO, +widthratio); // 1,. if (heightratio > 1 && widthratio > 1) if (heightratio > widthratio) //. bmpfactoryoptions.insamplesize = heightratio; else //. bmpfactoryoptions.insamplesize = widthratio; //. bmpfactoryoptions.injustdecodebounds = false; bmp = BitmapFactory.decodeStream(getContentResolver(). ë openinputstream(imagefileuri), null, bmpfactoryoptions); //. returnedimageview.setimagebitmap(bmp);
22 CHAPTER 1 catch (FileNotFoundException e) Log.v( ERROR,e.toString()); XML main xml <?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 ></ImageView> <TextView android:layout_width= wrap_content android:layout_height= wrap_content ë android:text= Title: android:id= @+id/titletextview ></TextView> <EditText android:layout_height= wrap_content android:id= @+id/titleedittext ë android:layout_width= fill_parent ></EditText> <TextView android:layout_width= wrap_content android:layout_height= wrap_content ë android:text= Description android:id= @+id/descriptiontextview ></TextView> <EditText android:layout_height= wrap_content android:layout_width= fill_parent ë android:id= @+id/descriptionedittext ></EditText> <Button android:layout_width= wrap_content android:layout_height= wrap_content ë android:id= @+id/takepicturebutton android:text= Take Picture ></Button> <Button android:layout_width= wrap_content android:layout_height= wrap_content ë android:id= @+id/savedatabutton android:text= Save Data ></Button> </LinearLayout> onactivityresult Camera Bitmap
23 ( MediaStore) MediaStore MediaStore URI Media.EXTERNAL_CONTENT_URI MediaStore Cursor MediaStore MediaStore Images Media String[] columns = Media.DATA, Media._ID, Media.TITLE, Media.DISPLAY_NAME ; managedquery URI WHERE WHERE ORDER BY onehourago 1970 1 1 System currentimemillis() 1970 1 1 1 000 60 60
24 CHAPTER 1 long onehourago = System.currentTimeMillis()/1000 - (60 * 60) WHERE String[] wherevalues = +onehourago; String[] columns = Media.DATA, Media._ID, Media.TITLE, Media.DISPLAY_NAME, Media.DATE_ADDED ; WHERE (?) ORDER BY cursor = managedquery(media.external_content_uri, columns, Media.DATE_ADDED + >?,ë wherevalues, Media.DATE_ADDED + ASC ); null Cursor cursor = managedquery(media.external_content_uri, columns, null, null, null); displaycolumnindex = cursor.getcolumnindexorthrow(mediastore.images.media.data); movetofirst Cursor
25 getstring getint if (cursor.movetofirst()) String displayname = cursor.getstring(displaycolumnindex); MediaStore package com.apress.proandroidmedia.ch1.mediastoregallery; import android.app.activity; import android.database.cursor; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.os.bundle; import android.provider.mediastore; import android.provider.mediastore.images.media; import android.util.log; import android.view.view; import android.view.view.onclicklistener; import android.widget.imagebutton; import android.widget.textview; public class MediaStoreGallery extends Activity public final static int DISPLAYWIDTH = 200; public final static int DISPLAYHEIGHT = 200;
26 CHAPTER 1 TextView titletextview; ImageButton imagebutton; ImageView ImageButton Button ( ) ImageView( ) Cursor cursor; Bitmap bmp; String imagefilepath; int filecolumn; int titlecolumn; int displaycolumn; @Override public void oncreate(bundle savedinstancestate) super.oncreate(savedinstancestate); setcontentview(r.layout.main); titletextview = (TextView) this.findviewbyid(r.id.titletextview); imagebutton = (ImageButton) this.findviewbyid(r.id.imagebutton); managedquery String[] columns = Media.DATA, Media._ID, Media.TITLE, Media.DISPLAY_NAME ; cursor = managedquery(media.external_content_uri, columns, null, null, null); Cursor Media DATA MediaStore Images Media DATA android provider MediaStore Images Media import Media DATA filecolumn = cursor.getcolumnindexorthrow(mediastore.images.media.data); titlecolumn = cursor.getcolumnindexorthrow(mediastore.images.media.title); displaycolumn = cursor.getcolumnindexorthrow(mediastore.images.media.display_name);
27 Cursor movetofirst if (cursor.movetofirst()) // titletextview.settext(cursor.getstring(titlecolumn)); titletextview.settext(cursor.getstring(displaycolumn)); imagefilepath = cursor.getstring(filecolumn); bmp = getbitmap(imagefilepath); //. imagebutton.setimagebitmap(bmp); ImageButton OnClickListener OnClickListener Cursor MoveToNext imagebutton.setonclicklistener(new OnClickListener() public void onclick(view v) if (cursor.movetonext()) // titletextview.settext(cursor.getstring(titlecolumn)); titletextview.settext(cursor.getstring(displaycolumn)); ); imagefilepath = cursor.getstring(filecolumn); bmp = getbitmap(imagefilepath); imagebutton.setimagebitmap(bmp); getbitmap
28 CHAPTER 1 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) DISPLAYHEIGHT); int widthratio = (int) Math.ceil(bmpFactoryOptions.outWidth ë / (float) DISPLAYWIDTH); Log.v( HEIGHTRATIO, + heightratio); Log.v( WIDTHRATIO, + 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; XML res/layout/main xml <?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
29 android:layout_height= fill_parent > <ImageButton android:layout_width= wrap_content android:layout_height= wrap_content ë android:id= @+id/imagebutton ></ImageButton> <TextView android:layout_width= fill_parent android:layout_height= wrap_content android:id= @+id/titletextview android:text= Image Title /> </LinearLayout> EXIF EXIF EXIF SD EXIF iphoto EXIF EXIF ExposureTime ShutterSpeedValue : : : :
30 CHAPTER 1 EXIF ExifInterface ExifInterface EXIF ExifInterface ei = new ExifInterface(imageFilePath); String imagedescription = ei.getattribute( ImageDescription ); if (imagedescription!= null) Log.v( EXIF, imagedescription); ExifInterface EXIF ExifInterface ei = new ExifInterface(imageFilePath); ei.setattribute( ImageDescription, Something New ); ExifInterface Camera EXIF 2010 4 2 3 www cipa jp/english/ hyoujunka/kikaku/pdf/dc 008 2010_E pdf Camera Camera
31 BitmapFactory MediaStore EXIF EXIF EXIF!