Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8450441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:58:10+00:00 2026-06-10T10:58:10+00:00

In my app I have an option that allows users to browse for audio

  • 0

In my app I have an option that allows users to browse for audio files on their phone to add to the app. I am having trouble however with creating a faster way of processing the query code. Currently it searches the entire external storage and causes the phone to prompt a force close/wait warning. I would like to take the code I have posted below and make it more efficient by either searching in a specific folder on the phone or by streamlining the process to make the file search quicker. I am not sure how to do this however. Thanks!

public class BrowseActivity extends DashboardActivity implements
    OnClickListener, OnItemClickListener {

private List<Sound> soundsInDevice = new ArrayList<Sound>();
private List<Sound> checkedList;
private ListView browsedList;
private BrowserSoundAdapter adapter;
private long categoryId;
private Category category;

private String currentCategoryName;
private String description;

//  private Category newCategory ;
private Button doneButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_browse);

    checkedList = new ArrayList<Sound>();

    browsedList = (ListView) findViewById(android.R.id.list);
    doneButton = (Button) findViewById(R.id.doneButton);

    soundsInDevice = getMediaSounds();

    if (soundsInDevice.size() > 0) {
        adapter = new BrowserSoundAdapter(this, R.id.browseSoundName,
                soundsInDevice);
    } else {
        Toast.makeText(getApplicationContext(),
                getString(R.string.no_sounds_available), Toast.LENGTH_SHORT)
                .show();
    }

    browsedList.setAdapter(adapter);
    browsedList.setOnItemClickListener(this);

     doneButton.setOnClickListener(this);
}

private List<Sound> getMediaSounds() {
    List<Sound> mediaSoundList = new ArrayList<Sound>();
    ContentResolver cr = getContentResolver();
    String[] projection = {MediaStore.Audio.Media._ID,
            MediaStore.Audio.Media.DISPLAY_NAME,
            MediaStore.Audio.Media.TITLE, 
            MediaStore.Audio.Media.DATA,
            MediaStore.Audio.Media.DURATION};

    final Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    Log.v("MediaStore.Audio.Media.EXTERNAL_CONTENT_URI", "" + uri);
    final Cursor cursor = cr.query(uri, projection, null, null, null);
    int n = cursor.getCount();
    Log.v("count", "" + n);
    if (cursor.moveToFirst()) {
        do {
            String soundName = cursor
                    .getString(cursor
                            .getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));
            Log.v("soundName", "" + soundName);
            String title = cursor
                    .getString(cursor
                            .getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));


            Log.v("title", "" + title);
            String path = cursor.getString(cursor
                    .getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));

            Log.v("path", "" + path);

            Sound browsedSound = new Sound(title, path, false, false,
                    false, false, 0);

            Log.v("browsedSound", "" + browsedSound);

            mediaSoundList.add(browsedSound);

            Log.v("mediaSoundList", "" + mediaSoundList.toString());
        } while (cursor.moveToNext());

    }
    return mediaSoundList;

}

public class BrowserSoundAdapter extends ArrayAdapter<Sound> {

    public BrowserSoundAdapter(Context context, int textViewResourceId,
            List<Sound> objects) {
        super(context, textViewResourceId, objects);

    }

    @Override
    public View getView(final int position, View convertView,
            ViewGroup parent) {
        ViewHolder viewHolder;
        View view = convertView;
        LayoutInflater inflater = getLayoutInflater();

        if (view == null) {
            view = inflater.inflate(R.layout.list_item_browse, null);
            viewHolder = new ViewHolder();

            viewHolder.soundNameTextView = (TextView) view
                    .findViewById(R.id.browseSoundName);
            viewHolder.pathTextView = (TextView) view
                    .findViewById(R.id.browseSoundPath);
            viewHolder.checkToAddSound = (CheckBox) view
                    .findViewById(R.id.browse_checkbox);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        final Sound sound = soundsInDevice.get(position);

        if (sound.isCheckedState()) {
            viewHolder.checkToAddSound.setChecked(true);
        } else {
            viewHolder.checkToAddSound.setChecked(false);
        }

        viewHolder.soundNameTextView.setText(sound.getName());
        viewHolder.pathTextView.setText(sound.getUri());

        viewHolder.checkToAddSound
                .setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        CheckBox cb = (CheckBox) v
                                .findViewById(R.id.browse_checkbox);
                        boolean checked = cb.isChecked();
                        boolean newValue = checked;
                        updateView(position, newValue);
                        doneButtonStatus(checkedList.size());
                    }
                });

        return view;
    }
}

// Adapter view holder class
private class ViewHolder {
    private TextView soundNameTextView;
    private TextView pathTextView;
    private CheckBox checkToAddSound;
}

// done button On Click
@Override
public void onClick(View view) {

    boolean status = getIntent().getBooleanExtra("FromAddCat", false);
    Log.v("for add category","enters in if");
    if(status){
        Log.v("for add category","enters in if1");
        currentCategoryName = getIntent().getStringExtra("categoryName");
        description = getIntent().getStringExtra("description");                                
        boolean existCategory = SQLiteHelper.getCategoryStatus(currentCategoryName);

        if (!existCategory) {
             category = new Category(currentCategoryName, description,
                    false);

            category.insert();
            category.update();          
            Log.v("for add category","enters in if2");
        }
    }else{
        categoryId = getIntent().getLongExtra("categoryId",-1);
        category = SQLiteHelper.getCategory(categoryId);
    }


    for (Sound checkedsound : checkedList) {
        checkedsound.setCheckedState(false);
        checkedsound.insert();
        category.getSounds().add(checkedsound);
        final Intent intent = new Intent(this, CategoriesActivity.class);
        finish();
        startActivity(intent);
    }
}

@Override
public void onItemClick(AdapterView<?> arg0, View view, int position,
        long arg3) {
    boolean checked = true;
    boolean newValue = false;
    CheckBox cb = (CheckBox) view.findViewById(R.id.browse_checkbox);
    if (cb.isChecked()) {
        cb.setChecked(!checked);
        newValue = !checked;
    } else {
        cb.setChecked(checked);
        newValue = checked;
    }
    updateView(position, newValue);
    doneButtonStatus(checkedList.size());

}

private void doneButtonStatus(int size) {

    if (size > 0) {
        doneButton.setEnabled(true);
        doneButton.setBackgroundResource(R.drawable.done_button_drawable);

    } else {
        doneButton.setEnabled(false);
        doneButton.setBackgroundResource(R.drawable.done_btn_disabled);
    }
}

private void updateView(int index, boolean newValue) {
    System.out.println(newValue);
    Sound sound = soundsInDevice.get(index);
    if (newValue == true) {
        checkedList.add(sound);
        sound.setCheckedState(newValue);
    } else {
        checkedList.remove(sound);
        sound.setCheckedState(newValue);
    }

}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T10:58:12+00:00Added an answer on June 10, 2026 at 10:58 am

    The issue is that you are performing the search in the application’s main thread that is responsible for updating the UI. Long running tasks are fine: you just need to do them in a background thread. You can use an AsyncTask for that or a worker thread.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an app that allows users to purchase publications that download to their
I have an app that allows users to enter a project into a database.
I have an app that allows users to enter, edit and search for projects
I have an ASP.NET forms app that allows users to run some management reports.
I want to make an app that allows users to add other users to
I have an iPhone app that has a 4 option menu, and allows the
I have an app that allows users to create and build a site. Let's
I have a Silverlight 3 app that that will let users download PDF files
Say you have an app, that you want to provide users ability to browse
I have a Silveright app that allows users to specify filters on a few

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.