Upgrade to Pro — share decks privately, control downloads, hide ads and more …

MediaStore

 MediaStore

Keishin Yokomaku

May 27, 2016
Tweet

More Decks by Keishin Yokomaku

Other Decks in Technology

Transcript

  1. MediaStore Keishin Yokomaku @ Drivemode, Inc. kyobashi.dex #3 

  2. @KeithYokoma • Keishin Yokomaku at Drivemode, Inc. • Work •

    Android apps • Android Training and its publication • Like • Bicycle, Photography, Tumblr and Motorsport • ·͓ͨલ͔ 
  3. MediaStore 

  4. MediaStore is Կ • ୺຤ʹอଘ͞ΕͨԻָɾը૾ɾಈըͷ؅ཧΛ͢Δσʔλϕʔε • MediaStore.Audio, MediaStore.Images, MediaStore.Video •

    Uri ͱ֤छͷΧϥϜ৘ใ͕ఏڙ͞Ε͍ͯΔ • EXTERNAL_CONTENT_URI ͱ INTERNAL_CONTENT_URI • MediaStore.Audio.Media ΍ MediaStore.Images.Media ͳͲ 
  5. MediaStore is Կ public class SampleActivity extends Activity { @Override

    protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Cursor albums = null; try { albums = getContentResolver().query( MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, null, null, null, null); } finally { if (albums != null) albums.close(); } } } 
  6. MediaStore is Կ public class SampleActivity extends Activity { @Override

    protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Cursor albums = null; try { albums = getContentResolver().query( MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, null, null, null, null); } finally { if (albums != null) albums.close(); } } } 
  7. Ṗଟ͖ MediaStore • ऴΘΒ͵ѱ͖͠ܧঝͷ࿈࠯ͱΧϥϜͷࠞಱ • ΧϥϜ৘ใʹܧঝؔ܎͸͋Δ͕ɺ਌Ͱఆٛ͞ΕͨΧϥϜ৘ใ͕ࢠͰ΋࢖ ͑Δͱ͸ݶΒͳ͍ • MediaColumns ͸Իָɾը૾ɾಈը͢΂ͯͷڞ௨ͷΧϥϜ৘ใΛ࣋ͬ

    ͍ͯΔ͕ৗʹͦͷΧϥϜ৘ใ͕࢖͑Δͱ͸ݶΒͳ͍ • ΧϥϜͷ੍໿͕ϫΧϥφΠ • Javadoc ʹॻ͍ͯͳ͍ͷͰ࢖ͬͯΈͯॳΊͯؾ͕ͭ͘ 
  8. Ṗଟ͖ MediaStore • Ҿ͖ํ͕͍·͍ͪෆ໌ͳσʔλ • ద੾ͳ Uri Λ࢖Θͳ͍ͱऔͬͯ͜Εͳ͍ΧϥϜ͕͋Δ • શΧϥϜΛऔಘͨ͠ͱ͖ʹԿ͕ೖͬͯΔ͔ϫΧϥφΠ

    • υΩϡϝϯτʹͳ͍ Uri ͕͋ͬͨΓɺͦΕΛ࢖Θͳ͍ͱ໘౗͍࣮͘͞૷ Λ͠ͳ͍ͱ͍͚ͳ͔ͬͨΓ͢Δ • ΄΅ϊʔίϝϯτɾυΩϡϝϯτ 
  9. Ṗଟ͖ MediaStore • ੲͷΪϟϥϦʔΞϓϦ͸ MediaStore ʹ SQL ΠϯδΣΫγϣϯΛ͍ͯͨ͠ • ࣸਅΛσΟϨΫτϦ͝ͱʹ෼͚Δ֓೦͸ը૾ͻͱͭͻͱͭͷσʔλʹ݁

    ߹͍ͯ͠Δ • άϧʔϓԽ͠ͳ͍ͱσΟϨΫτϦͷ֓೦͕ͱΓͩͤͳ͍ • ContentResolver ʹάϧʔϓԽͷ API ͸ͳ͍ • SQL ΠϯδΣΫγϣϯͰରԠ͢Δߥۀ 
  10. ΋͏ŵŧƄແཧɻϦεΧ͠ΐ…… 

  11. Ͳ͏͢Δ • ະ஌ͳ෦෼͕ଟ͍ͱ͸͍͑ɺ෼͔ͬͯ͠·͑͹Ҿ͖ํ͸ҰఆʹͳΔ • ϘΠϥʔϓϨʔτԽ͢Δͷ͸໨ʹݟ͍͑ͯΔ • Cursor Λͦͷ··࢖͏ͷ͸ؾ͕໓ೖΔ • ·ͩ

    ContentResolver Ͱফ໣͢Δͭ΋Γͳͷʁ • ੍໿৘ใ͸ϝιουͷҾ਺ͱͯ͠ඞͣࢦఆ͢ΔΑ͏ʹͯ͠ݟ͑ΔԽ͍ͨ͠ 
  12. ΋͏ŵŧƄແཧɻϥΠϒϥϦʹ͠ΐ 

  13. MediaFacade 

  14. MediaFacade • https://github.com/Drivemode/MediaFacade • ·ͩϦϦʔε͸͍ͯ͠ͳ͍ • Ұ௨ΓԻָɾը૾ɾಈըͷ API ͕ͦΖ͍ͬͯΔ •

    ΈΜͳେ޷͖γϯάϧτϯ • Cursor ͷྑ͞Λ׆͔ͭͭ͠ɺσʔλ΁ͷΞΫηεΛ͠΍͘͢ 
  15. ྫɿΞϧόϜ(Իָ)ͷऔಘ public class SampleActivity extends Activity { private AudioFacade facade;

    @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); facade = AudioFacade.getInstance(this); Cursor albums = null; try { albums = facade.album().fetchAlbum(); // do whatever you like with the album metadata } finally { if (albums != null) albums.close(); } } } 
  16. ྫɿΞϧόϜ(Իָ)ͷऔಘ public class SampleActivity extends Activity { private AudioFacade facade;

    @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); facade = AudioFacade.getInstance(this); Cursor albums = null; try { albums = facade.album().fetchAlbum(); // do whatever you like with the album metadata } finally { if (albums != null) albums.close(); } } } 
  17. ྫɿΞϧόϜ(Իָ)ͷऔಘ public class SampleActivity extends Activity { private AudioFacade facade;

    @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); facade = AudioFacade.getInstance(this); Cursor albums = null; try { albums = facade.album().fetchAlbum(); // do whatever you like with the album metadata } finally { if (albums != null) albums.close(); } } } 
  18. ྫɿΞϧόϜ(Իָ)ͷऔಘ AlbumCursor albums = null; try { albums = facade.album().fetchAlbum();

    while (albums.moveToNext()) { long id = albums.id(); String name = albums.displayName(); // …… } } finally { if (albums != null) albums.close(); } 
  19. ྫɿΞϧόϜ(Իָ)ͷऔಘ AlbumCursor albums = null; try { albums = facade.album().fetchAlbum();

    while (albums.moveToNext()) { long id = albums.id(); String name = albums.displayName(); // …… } } finally { if (albums != null) albums.close(); } 
  20. Α͞ • ContentResolver ܦ༝Ͱͷૢ࡞Λϥοϓ͠ɺԿΛҾ͖͍͔ͨ͝ͱʹϝιο υ͕Θ͔Ε͍ͯΔͷͰαοͱ࢖͑Δ • ୯७ͳߏ଄ͳͷͰ؆୯ʹ Rx ͳͲͰϥοϓͰ͖Δ •

    طଘͷ Android ͷ API ͱͷ਌࿨ੑ͸΄΅ͦͷ··(Loader ͸͋·Γߟ͑ͯ ͍ͳ͍) • औ͖ͬͯͨ Cursor ͔ΒϝιουҰൃͰͱΓ͍ͨσʔλʹΞΫηεͰ͖Δ • ൥Θ͍͠ΧϥϜ໊ࢦఆ͔ΒαϤφϥʂ 
  21. ॿ͚ͯཉ͍͜͠ͱ • ୺຤ґଘ͕͋Δͧ • ṖͷΧϥϜ͕ੜ͑ͯͨΓɺṖͷσʔλ͕ೖ͍ͬͯͨΓ • ΧϥϜ͕૿͍͑ͯΔ෼ʹ͸େͯ͠ࠔΒͳ͍͕ɺຊདྷΧϥϜʹೖ͍ͬͯΔ ͸ͣͷͳ͍σʔλ͕ೖ͍ͬͯΔͱ͖͸ࠔΔ • OSS

    ΁ίϯτϦϏϡʔτνϟϯεʂ 
  22. MediaStore Keishin Yokomaku @ Drivemode, Inc. kyobashi.dex #3