Last active
May 26, 2020 14:27
-
-
Save HoussemNasri/7905ea1c71099ddeba6b6bacd60a1a23 to your computer and use it in GitHub Desktop.
Android : fetch all music on device
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context | |
import android.database.Cursor | |
import android.provider.MediaStore | |
import android.util.Log | |
class AudioDataSource { | |
companion object { | |
fun getAllAudioFromDevice(context: Context): List<AudioModel> { | |
val tempAudioList: MutableList<AudioModel> = ArrayList() | |
val uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI | |
val projection = arrayOf( | |
MediaStore.Audio.AudioColumns.DATA, | |
MediaStore.Audio.AudioColumns.TITLE, | |
MediaStore.Audio.AudioColumns.ALBUM, | |
MediaStore.Audio.ArtistColumns.ARTIST | |
) | |
val selection = "${MediaStore.Audio.Media.IS_MUSIC} != 0" | |
val c: Cursor? = context.getContentResolver().query( | |
uri, | |
projection, | |
selection, | |
null, | |
null | |
) | |
if (c != null) { | |
while (c.moveToNext()) { | |
val audioModel = AudioModel() | |
val path: String = c.getString(0) | |
val name: String = c.getString(1) | |
val album: String = c.getString(2) | |
val artist: String = c.getString(3) | |
audioModel.aName = name | |
audioModel.aAlbum = album | |
audioModel.aArtist = artist | |
audioModel.aPath = path | |
Log.e("Name :$name", " Album :$album") | |
Log.e("Path :$path", " Artist :$artist") | |
tempAudioList.add(audioModel) | |
} | |
c.close() | |
} | |
return tempAudioList | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AudioModel( | |
var aPath: String = "", | |
var aName: String = "", | |
var aAlbum: String = "", | |
var aArtist: String = "" | |
) { | |
override fun toString(): String { | |
return "AudioModel(aPath='$aPath', aName='$aName', aAlbum='$aAlbum', aArtist='$aArtist')" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//request permissions | |
Dexter.withContext(this) | |
.withPermission(Manifest.permission.READ_EXTERNAL_STORAGE) | |
.withListener(object : PermissionListener { | |
override fun onPermissionGranted(p0: PermissionGrantedResponse?) { | |
Log.d(TAG, "Granted") | |
val allAudioFromDevice = | |
AudioDataSource.getAllAudioFromDevice(this@ServicesActivity) | |
Log.d(TAG, "Audio File : ${allAudioFromDevice.size}") | |
allAudioFromDevice.forEach { Log.d(TAG, it.toString()) } | |
} | |
override fun onPermissionRationaleShouldBeShown( | |
p0: PermissionRequest?, | |
p1: PermissionToken? | |
) {} | |
override fun onPermissionDenied(p0: PermissionDeniedResponse?) {} | |
}) | |
.check() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment