2012-07-25 25 views
0

在我的應用程序中,我有一個選項,允許用戶瀏覽手機上的音頻文件以添加到應用程序。然而,我創建處理查詢代碼的更快方式遇到了麻煩。目前它搜索整個外部存儲並使手機提示強制關閉/等待警告。我想將我在下面發佈的代碼拿來,並通過在手機上的特定文件夾中搜索或通過簡化流程以使文件搜索更快來提高效率。但我不知道如何做到這一點。謝謝!如何在MediaStore中搜索特定目錄而不是整個外部存儲?

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

問題是,您正在應用程序的主線程中執行搜索,負責更新UI。長時間運行的任務很好:你只需要在後臺線程中完成它們。您可以使用AsyncTaskworker thread

相關問題