Last active
February 15, 2022 10:45
-
-
Save kaecy/6b9a190aa25578d4370a0c5d5a09edc8 to your computer and use it in GitHub Desktop.
List All Audio Files in Android - modifications to this https://github.com/kaecy/android-demos/tree/master/adding%20search
This file contains 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
package org.cheat.search; | |
import android.content.Intent; | |
import android.content.Context; | |
import android.net.Uri; | |
import android.widget.TextView; | |
import android.widget.ListView; | |
import android.provider.MediaStore.Audio; | |
public class OpenLinksListener implements ListView.OnItemClickListener { | |
Context context; | |
public OpenLinksListener(Context context) | |
{ | |
this.context = context; | |
} | |
@Override | |
public void onItemClick(android.widget.AdapterView<?> parent, android.view.View view, int position, long id) | |
{ | |
Uri openable = Uri.parse(Audio.Media.getContentUri("external").toString() + "/" + (((TextView)view).getText().toString().split(": ")[0])); | |
Intent intent = new Intent(Intent.ACTION_VIEW); | |
intent.setDataAndType(openable, "audio/*"); | |
context.startActivity(intent); | |
} | |
} |
This file contains 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
package org.cheat.search; | |
import android.app.Activity; | |
import android.os.Bundle; | |
import android.widget.SearchView; | |
import android.widget.ListView; | |
import android.widget.ArrayAdapter; | |
import android.database.Cursor; | |
public class Search extends Activity | |
{ | |
/** Called when the activity is first created. */ | |
@Override | |
public void onCreate(Bundle savedInstanceState) | |
{ | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.search_result_layout); | |
activity = this; | |
adapter = new ArrayAdapter(this, R.layout.textview); | |
ListView listview = (ListView)findViewById(R.id.listview); | |
listview.setAdapter(adapter); | |
listview.setOnItemClickListener(new OpenLinksListener(activity)); | |
} | |
public void performSearch(String query) | |
{ | |
Cursor music = getContentResolver().query( | |
android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, | |
new String[]{ "_id", "_data" }, | |
"is_music != 0", | |
null, null); | |
adapter.clear(); // clear all the items in the listview | |
query = query.toLowerCase(); // we want the match to be case-insensitive | |
String path; | |
while ( music.moveToNext() ) | |
{ | |
path = music.getString(1).toLowerCase(); | |
if ( path.contains(query) ) | |
adapter.add(music.getString(0) + ": " + music.getString(1)); | |
} | |
adapter.notifyDataSetChanged(); // now we tell the adapter to update the changes | |
} | |
@Override | |
public boolean onCreateOptionsMenu(android.view.Menu activityMenu) | |
{ | |
getMenuInflater().inflate(R.menu.actions, activityMenu); | |
SearchView search = (SearchView) activityMenu.findItem(R.id.search).getActionView(); | |
search.setOnQueryTextListener(new SearchView.OnQueryTextListener() { | |
@Override | |
public boolean onQueryTextSubmit(String text) { | |
activity.performSearch(text); | |
return true; | |
} | |
@Override | |
public boolean onQueryTextChange(String text) { | |
return false; | |
} | |
}); | |
return true; | |
} | |
Search activity; | |
ArrayAdapter adapter; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment