在我的应用程序中,我有一个选项允许用户浏览手机上的音频文件以添加到应用程序中。但是,我在创建更快的查询代码处理方式时遇到了麻烦。目前它会搜索整个外部存储并导致手机提示强制关闭/等待警告。我想采用我在下面发布的代码,并通过在手机上的特定文件夹中搜索或通过简化流程以加快文件搜索速度来提高效率。但是,我不确定如何执行此操作。谢谢!
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);
}
}
}