android SoundRecorder (录音机)添加录音列表菜单项
在录音机中添加一个录音列表项,点击后用户可以看到已经存在的录音文件。
效果图如下:
修改代码参照如下:
最初的录音机是没有录音列表选项的,所以我们要加上去。
SoundRecorder\res\menu\main_menu.xml添加录音菜单选项
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/menu_item_filetype"
android:title="@string/format_setting"/>
<item
android:id="@+id/menu_item_storage"
android:title="@string/storage_setting"/>
<item
android:id="@+id/menu_item_keyboard"
android:title="@string/keyboard"/>
<item
android:id="@+id/menu_item_list"
android:title="@string/audio_db_playlist_name"/>
</menu>
SoundRecorder\src\com\android\soundrecorder\SoundRecorder.java 添加对菜单项的响应
public static final String SOUNDRECORDER_PLAYLIST = "My recordings";
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.menu_item_keyboard:
if(mRecorder.state() == Recorder.IDLE_STATE) {
InputMethodManager inputMgr =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMgr.toggleSoftInput(0, 0);
}
break;
case R.id.menu_item_filetype:
if(mRecorder.state() == Recorder.IDLE_STATE) {
openOptionDialog(SETTING_TYPE_FILE_TYPE);
}
break;
case R.id.menu_item_storage:
if(mRecorder.state() == Recorder.IDLE_STATE) {
openOptionDialog(SETTING_TYPE_STORAGE_LOCATION);
}
break;
case R.id.menu_item_list:
Intent intent = new Intent(this, RecordingList.class);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
/SoundRecorder/src/com/android/soundrecorder/RecordingList.java添加展示列表的activity
package com.android.soundrecorder;
import android.app.ListActivity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.CursorAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.content.res.Resources;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ListView;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import java.io.File;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnClickListener;
import android.app.Dialog;
import android.app.AlertDialog;
import android.widget.EditText;
import android.text.InputFilter;
public class RecordingList extends ListActivity {
static final String TAG = "RecordingList";
private static final int MAX_FILE_NAME_LENGTH = 80;
private ListView mTrackList;
private static final int MENU_RENAME = 0;
private static final int MENU_DELETE = 1;
private static final int DIALOG_DELETE = 0;
private String mCurrentRecordingTitle;
private long mSelectedId;
private String mSelectedName;
private SimpleCursorAdapter mAdapter;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
init();
mTrackList = getListView();
mTrackList.setOnCreateContextMenuListener(this);
if (icicle != null) {
onRestoreInstanceState(icicle);
}
}
@Override
protected void onSaveInstanceState(Bundle out) {
super.onSaveInstanceState(out);
out.putString("mSelectedName", mSelectedName);
out.putLong("mSelectedId", mSelectedId);
}
protected void onRestoreInstanceState(Bundle in) {
mSelectedName = in.getString("mSelectedName");
mSelectedId = in.getLong("mSelectedId");
}
public void init() {
setContentView(R.layout.recording_list);
makeCursor();
updateRecordingList(mCursor);//add by xiaoya 2014/06/17 for [bugId:VK85000000165]
setTitle(R.string.audio_db_playlist_name);
mAdapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, mCursor,
new String[] { MediaStore.Video.Media.DISPLAY_NAME },
new int[] { android.R.id.text1 });
setListAdapter(mAdapter);
}
/**
* add by xiaoya 2014/06/17 for [bugId:VK85000000165]
* @param cursor
*/
private void updateRecordingList(Cursor cursor) {
if (null != cursor) {
cursor.moveToFirst();
while (cursor.moveToNext()) {
mSelectedName = cursor.getString(cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
mSelectedId = cursor.getLong(cursor
.getColumnIndexOrThrow(MediaStore.Audio.Playlists.Members.AUDIO_ID));
try {
File f=new File(mSelectedName);
if (!f.exists()) {
delete();
}
} catch (Exception e) {
return;
}
}
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Intent intent = new Intent(Intent.ACTION_VIEW);
mCursor.moveToPosition(position);
String type = mCursor.getString(mCursor
.getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE));
mSelectedName = mCursor.getString(mCursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
//add by xiaoya 2014/06/11 for [bugId:VK85000000165] begin
try {
File f=new File(mSelectedName);
if (!f.exists()) {
Toast.makeText(v.getContext(), "This file has been removed!", Toast.LENGTH_LONG).show();
return;
}
} catch (Exception e) {
return;
}
//add by xiaoya 2014/06/11 for [bugId:VK85000000165] end
intent.setDataAndType(Uri.fromFile(new File(mSelectedName)), type);
startActivity(intent);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View view,
ContextMenuInfo menuInfoIn) {
menu.add(0, MENU_DELETE, 0, R.string.menu_delete);
AdapterContextMenuInfo mi = (AdapterContextMenuInfo) menuInfoIn;
mCursor.moveToPosition(mi.position);
try {
mCurrentRecordingTitle = mCursor.getString(mCursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
mSelectedName = mCursor.getString(mCursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
int id_idx = mCursor
.getColumnIndexOrThrow(MediaStore.Audio.Playlists.Members.AUDIO_ID);
mSelectedId = mCursor.getLong(id_idx);
} catch (IllegalArgumentException ex) {
mSelectedId = mi.id;
}
menu.setHeaderTitle(mCurrentRecordingTitle);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_DELETE: {
showDialog(DIALOG_DELETE);
return true;
}
}
return super.onContextItemSelected(item);
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_DELETE: {
return new AlertDialog.Builder(this)
.setTitle(getString(R.string.really_delete))
.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton(android.R.string.ok,
new OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
delete();
}
}).setNegativeButton(android.R.string.cancel, null)
.create();
}
}
return null;
}
private void delete() {
StringBuilder where = new StringBuilder();
where.append(MediaStore.Audio.Media._ID + " IN (");
where.append(mSelectedId);
where.append(")");
getContentResolver().delete(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, where.toString(),
null);
File f = new File(mSelectedName);
Log.e(TAG, "delete file " + mSelectedName + ", mSelectedId:"
+ mSelectedId);
if (!f.delete()) {
Log.e(TAG, "Failed to delete file " + mSelectedName);
}
}
private void makeCursor() {
String[] cols = new String[] { MediaStore.Audio.Playlists.Members._ID,
MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Playlists.Members.AUDIO_ID,
MediaStore.Audio.Media.MIME_TYPE, };
if (mCursor != null) {
mCursor.close();
mCursor = null;
}
ContentResolver resolver = getContentResolver();
if (resolver == null) {
Log.e(TAG, "resolver = null");
} else if (-1 == getPlaylistId()) {
Log.e(TAG, "invalid playlist id: -1");
} else {
Uri uri = MediaStore.Audio.Playlists.Members.getContentUri(
"external", Long.valueOf(getPlaylistId()));
mCursor = resolver.query(uri, cols, null, null, mSortOrder);
}
}
private int getPlaylistId() {
Resources res = getResources();
Uri uri = MediaStore.Audio.Playlists.getContentUri("external");
final String[] ids = new String[] { MediaStore.Audio.Playlists._ID };
final String where = MediaStore.Audio.Playlists.NAME + "=?";
final String[] args = new String[] {
//SoundRecorder.SOUNDRECORDER_PLAYLIST
"My recordings"
};
Cursor cursor = getContentResolver().query(uri, ids, where, args, null);
if (cursor == null) {
Log.v(TAG, "query returns null");
}
int id = -1;
if (cursor != null) {
cursor.moveToFirst();
if (!cursor.isAfterLast()) {
id = cursor.getInt(0);
}
cursor.close();
}
return id;
}
@Override
public void onDestroy() {
if (mCursor != null) {
mCursor.close();
mCursor = null;
}
super.onDestroy();
}
private Cursor mCursor;
private String mWhereClause;
private String mSortOrder;
}
最后增加列表的布局文件:/SoundRecorder/res/layout/recording_list.xml
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ListView android:id="@id/android:list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:drawSelectorOnTop="true" /> </LinearLayout></span>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。