How do I search the MediaStore for a specific directory instead of entire external storage?

Posted by Nick Lopez on Stack Overflow See other posts from Stack Overflow or by Nick Lopez
Published on 2012-07-25T07:00:35Z Indexed on 2012/08/28 3:38 UTC
Read the original article Hit count: 164

Filed under:

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

}
}

© Stack Overflow or respective owner

Related posts about android