前言
欢迎大家我分享和推荐好用的代码段~~
声明
欢迎转载,但请保留文章原始出处:
CSDN:http://www.csdn.net
雨季o莫忧离:http://blog.csdn.net/luckkof
正文
Step1: AndroidManifest.xml
Add use permission
<uses-permission android:name="android.permission.WRITE_SETTING"/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
Step2: resource
res\menu\popup_menu.xml
<item android:id="@+id/set_ringtone"
android:title="@string/set_ringtone"/>
res\values\strings.xml
<string name="set_ringtone">Use As Ringtone</string>
Use As Ringtone
多国语言需要添加在对应的values_xxxx\strings.xml 中
Step3:import
FileManagerOperationActivity.java
import android.provider.MediaStore;
import android.database.Cursor;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.provider.Settings;
import android.widget.Toast;
import android.content.ContentUris;
import android.media.MediaFile;
Step4:显示、隐藏setRingtone菜单
updateEditPopupMenu() 方法改为如下
private void updateEditPopupMenu() {
final Menu menu = mEditPopupMenu.getMenu();
int selectedCount = mAdapter.getCheckedItemsCount();
// remove (disable) protection info icon
menu.removeItem(R.id.protection_info);
if (selectedCount == 0) {
menu.findItem(R.id.rename).setEnabled(false);
menu.findItem(R.id.details).setEnabled(false);
//add
menu.findItem(R.id.set_ringtone).setEnabled(false);
} else if (selectedCount == 1) {
// enable details icon
menu.findItem(R.id.details).setEnabled(true);
// enable rename icon
if (mAdapter.getCheckedFileInfoItemsList().get(0).getFile().canWrite()) {
menu.findItem(R.id.rename).setEnabled(true);
}
//add
// only audio file can enable set ringtone
FileInfo fileInfo1 = mAdapter.getCheckedFileInfoItemsList().get(0);
if(!fileInfo1.isDrmFile()
&& !fileInfo1.isDirectory()
&& IsMusic(fileInfo1)){
LogUtils.d(TAG, "need set ringtone" );
menu.findItem(R.id.set_ringtone).setEnabled(true);
}else {
menu.findItem(R.id.set_ringtone).setEnabled(false);
}
// enable protection info icon
FileInfo fileInfo = mAdapter.getCheckedFileInfoItemsList().get(0);
if (fileInfo.isDrmFile()) {
String path = fileInfo.getFileAbsolutePath();
if (DrmManager.getInstance().checkDrmObjectType(path)) {
String mimeType = DrmManager.getInstance().getOriginalMimeType(path);
if (mimeType != null && mimeType.trim().length() != 0) {
menu.add(0, R.id.protection_info, 0,
com.mediatek.internal.R.string.drm_protectioninfo_title);
}
}
}
} else {
// disable details icon
menu.findItem(R.id.details).setEnabled(false);
// disable rename icon
menu.findItem(R.id.rename).setEnabled(false);
//add
menu.findItem(R.id.set_ringtone).setEnabled(false);
}
}
Step5: .判断选择文件是否是audio 文件
定义IsMusic 方法
private boolean IsMusic(FileInfo fileinfo ){
String mimeType = fileinfo.getFileMimeType(mService);
int fileType = MediaFile.getFileTypeForMimeType(mimeType);
if (mimeType.startsWith("audio/") || MediaFile.isAudioFileType(fileType)) {
return true;
}else {
return false;
}
}
Step6: 定义设置ringtone 方法
新增方法 setRingtone
public void setRingtone(MenuItem menuItem){
LogUtils.d(TAG, "start setRingtone");
FileInfo fileinfo = mAdapter.getCheckedFileInfoItemsList().get(0);
ContentResolver resolver = getApplicationContext().getContentResolver();
String filepath = fileinfo.getFileAbsolutePath();
Long fileid = 0L;
//Uri fileuri=Uri fileuri= getUriForPath(filepath);
Uri fileuri= MediaStore.Audio.Media.getContentUriForPath(filepath);
LogUtils.d(TAG, "setRingtone uri"+fileuri);
LogUtils.d(TAG, "setRingtone path"+filepath);
Cursor file_cursor = resolver.query(
fileuri,
null,
MediaStore.MediaColumns.DATA+"=?",
new String[]{filepath},
null);
if(file_cursor.moveToFirst() && file_cursor.getCount() >0){
String _id = file_cursor.getString(0);
fileid = Long.valueOf(_id);
}
Uri ringUri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
fileid);
try {
ContentValues values = new ContentValues(2);
values.put(MediaStore.Audio.Media.IS_RINGTONE, "1");
values.put(MediaStore.Audio.Media.IS_ALARM, "1");
resolver.update(ringUri, values, null, null);
} catch (UnsupportedOperationException ex) {
LogUtils.d(TAG, "couldn't set ringtone flag for id " + ringUri);
return;
}
LogUtils.d(TAG, "setRingtone ringUri"+ringUri.toString());
String[] cols = new String[] {
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.TITLE
};
/// M: use selectionArgs replace set query value in where @{
String where = MediaStore.Audio.Media._ID + "=?";
String[] whereArgs = new String[]{String.valueOf(fileid)};
Cursor cursor = resolver.query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
cols,
where ,
whereArgs,
null);
/// @}
try {
if (cursor != null && cursor.getCount() == 1) {
// Set the system setting to make this the current ringtone
cursor.moveToFirst();
Settings.System.putString(resolver, Settings.System.RINGTONE, ringUri.toString());
Toast.makeText(getApplicationContext(), "setRingtone Success", Toast.LENGTH_SHORT).show();
}
} finally {
if (cursor != null) {
cursor.close();
}
}
}
Step7:重新定义MediaStore.java getContentUriForPath 方法
之所以定义getContentUriForPath,是因为当时开发该feature的时候,发现getContentUriForPath这个函数有bug,当路径是/storage/sdcard1/ 时候,无法正确返回uri,返回来的竟然是Internal_content_uri。
public static Uri getContentUriForPath(String path) {
return((path.startsWith(Environment.getRootDirectory()+"/media"))?
INTERNAL_CONTENT_URI :EXTERNAL_CONTENT_URI);
}
Step8:响应setRingtone
在onMenuItemClick 方法里面增加
case R.id.set_ringtone:
setRingtone(menuItem);
break;
最后编译
./mk mm ./mediatek/packages/apps/FileManager/