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

    View full-size slide

  2. @KeithYokoma
    • Keishin Yokomaku at Drivemode, Inc.
    • Work
    • Android apps
    • Android Training and its publication
    • Like
    • Bicycle, Photography, Tumblr and Motorsport
    • ·͓ͨલ͔

    View full-size slide

  3. MediaStore is Կ
    • ୺຤ʹอଘ͞ΕͨԻָɾը૾ɾಈըͷ؅ཧΛ͢Δσʔλϕʔε
    • MediaStore.Audio, MediaStore.Images, MediaStore.Video
    • Uri ͱ֤छͷΧϥϜ৘ใ͕ఏڙ͞Ε͍ͯΔ
    • EXTERNAL_CONTENT_URI ͱ INTERNAL_CONTENT_URI
    • MediaStore.Audio.Media ΍ MediaStore.Images.Media ͳͲ

    View full-size slide

  4. 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();
    }
    }
    }

    View full-size slide

  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();
    }
    }
    }

    View full-size slide

  6. Ṗଟ͖ MediaStore
    • ऴΘΒ͵ѱ͖͠ܧঝͷ࿈࠯ͱΧϥϜͷࠞಱ
    • ΧϥϜ৘ใʹܧঝؔ܎͸͋Δ͕ɺ਌Ͱఆٛ͞ΕͨΧϥϜ৘ใ͕ࢠͰ΋࢖
    ͑Δͱ͸ݶΒͳ͍
    • MediaColumns ͸Իָɾը૾ɾಈը͢΂ͯͷڞ௨ͷΧϥϜ৘ใΛ࣋ͬ
    ͍ͯΔ͕ৗʹͦͷΧϥϜ৘ใ͕࢖͑Δͱ͸ݶΒͳ͍
    • ΧϥϜͷ੍໿͕ϫΧϥφΠ
    • Javadoc ʹॻ͍ͯͳ͍ͷͰ࢖ͬͯΈͯॳΊͯؾ͕ͭ͘

    View full-size slide

  7. Ṗଟ͖ MediaStore
    • Ҿ͖ํ͕͍·͍ͪෆ໌ͳσʔλ
    • ద੾ͳ Uri Λ࢖Θͳ͍ͱऔͬͯ͜Εͳ͍ΧϥϜ͕͋Δ
    • શΧϥϜΛऔಘͨ͠ͱ͖ʹԿ͕ೖͬͯΔ͔ϫΧϥφΠ
    • υΩϡϝϯτʹͳ͍ Uri ͕͋ͬͨΓɺͦΕΛ࢖Θͳ͍ͱ໘౗͍࣮͘͞૷
    Λ͠ͳ͍ͱ͍͚ͳ͔ͬͨΓ͢Δ
    • ΄΅ϊʔίϝϯτɾυΩϡϝϯτ

    View full-size slide

  8. Ṗଟ͖ MediaStore
    • ੲͷΪϟϥϦʔΞϓϦ͸ MediaStore ʹ SQL ΠϯδΣΫγϣϯΛ͍ͯͨ͠
    • ࣸਅΛσΟϨΫτϦ͝ͱʹ෼͚Δ֓೦͸ը૾ͻͱͭͻͱͭͷσʔλʹ݁
    ߹͍ͯ͠Δ
    • άϧʔϓԽ͠ͳ͍ͱσΟϨΫτϦͷ֓೦͕ͱΓͩͤͳ͍
    • ContentResolver ʹάϧʔϓԽͷ API ͸ͳ͍
    • SQL ΠϯδΣΫγϣϯͰରԠ͢Δߥۀ

    View full-size slide

  9. ΋͏ŵŧƄແཧɻϦεΧ͠ΐ……

    View full-size slide

  10. Ͳ͏͢Δ
    • ະ஌ͳ෦෼͕ଟ͍ͱ͸͍͑ɺ෼͔ͬͯ͠·͑͹Ҿ͖ํ͸ҰఆʹͳΔ
    • ϘΠϥʔϓϨʔτԽ͢Δͷ͸໨ʹݟ͍͑ͯΔ
    • Cursor Λͦͷ··࢖͏ͷ͸ؾ͕໓ೖΔ
    • ·ͩ ContentResolver Ͱফ໣͢Δͭ΋Γͳͷʁ
    • ੍໿৘ใ͸ϝιουͷҾ਺ͱͯ͠ඞͣࢦఆ͢ΔΑ͏ʹͯ͠ݟ͑ΔԽ͍ͨ͠

    View full-size slide

  11. ΋͏ŵŧƄແཧɻϥΠϒϥϦʹ͠ΐ


    View full-size slide

  12. MediaFacade
    • https://github.com/Drivemode/MediaFacade
    • ·ͩϦϦʔε͸͍ͯ͠ͳ͍
    • Ұ௨ΓԻָɾը૾ɾಈըͷ API ͕ͦΖ͍ͬͯΔ
    • ΈΜͳେ޷͖γϯάϧτϯ
    • Cursor ͷྑ͞Λ׆͔ͭͭ͠ɺσʔλ΁ͷΞΫηεΛ͠΍͘͢

    View full-size slide

  13. ྫɿΞϧόϜ(Իָ)ͷऔಘ
    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();
    }
    }
    }

    View full-size slide

  14. ྫɿΞϧόϜ(Իָ)ͷऔಘ
    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();
    }
    }
    }

    View full-size slide

  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();
    }
    }
    }

    View full-size slide

  16. ྫɿΞϧόϜ(Իָ)ͷऔಘ
    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();
    }

    View full-size slide

  17. ྫɿΞϧόϜ(Իָ)ͷऔಘ
    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();
    }

    View full-size slide

  18. Α͞
    • ContentResolver ܦ༝Ͱͷૢ࡞Λϥοϓ͠ɺԿΛҾ͖͍͔ͨ͝ͱʹϝιο
    υ͕Θ͔Ε͍ͯΔͷͰαοͱ࢖͑Δ
    • ୯७ͳߏ଄ͳͷͰ؆୯ʹ Rx ͳͲͰϥοϓͰ͖Δ
    • طଘͷ Android ͷ API ͱͷ਌࿨ੑ͸΄΅ͦͷ··(Loader ͸͋·Γߟ͑ͯ
    ͍ͳ͍)
    • औ͖ͬͯͨ Cursor ͔ΒϝιουҰൃͰͱΓ͍ͨσʔλʹΞΫηεͰ͖Δ
    • ൥Θ͍͠ΧϥϜ໊ࢦఆ͔ΒαϤφϥʂ

    View full-size slide

  19. ॿ͚ͯཉ͍͜͠ͱ
    • ୺຤ґଘ͕͋Δͧ
    • ṖͷΧϥϜ͕ੜ͑ͯͨΓɺṖͷσʔλ͕ೖ͍ͬͯͨΓ
    • ΧϥϜ͕૿͍͑ͯΔ෼ʹ͸େͯ͠ࠔΒͳ͍͕ɺຊདྷΧϥϜʹೖ͍ͬͯΔ
    ͸ͣͷͳ͍σʔλ͕ೖ͍ͬͯΔͱ͖͸ࠔΔ
    • OSS ΁ίϯτϦϏϡʔτνϟϯεʂ

    View full-size slide

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

    View full-size slide