问题描述
我正在开发一个应用程序:我试图设置一个音频文件作为铃声。
我看到很多帖子,但没有人真正帮助我,所以我决定提出这个问题。 p>
我点击按钮时使用这段代码:
@Override
public void onClick(View v){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType(audio / *);
startActivityForResult(Intent.createChooser(intent,选择声音文件),音频);
在 onActivityResult
I试图获取文件路径,然后使用下面的代码将音频文件设置为铃声:
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data){
if(requestCode == Audio&&& requestCode == RESULT_OK){
Uri s1 = data .getData();
String s = s1.getPath();
$ b $ if(s!= null){
try {
k = new File(new URI(s)); //(File k;)
} catch(URISyntaxException e){
e.printStackTrace();
}
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA,k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE,My Song title);
values.put(MediaStore.MediaColumns.MIME_TYPE,audio / mpeg);
values.put(MediaStore.Audio.Media.ARTIST,Some Artist);
values.put(MediaStore.Audio.Media.IS_RINGTONE,false);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION,true);
values.put(MediaStore.Audio.Media.IS_ALARM,false);
values.put(MediaStore.Audio.Media.IS_MUSIC,false);
//将其插入数据库
Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
Uri newUri = getContentResolver()。insert(uri,values);
RingtoneManager.setActualDefaultRingtoneUri(
MainActivity.this,
RingtoneManager.TYPE_RINGTONE,
newUri);
code
我会很感激你的答案。对不起我的英文不好。解决方案我看到很多帖子,但是任何人都显示我应该做的。所以我决定创建一个完整的答案,其中我有我的问题的解决方案...
这是我的 MainActivity.java code>我使用的是
$ $ p $ import java.io.File;
导入android.app.Activity;
导入android.content.ContentValues;
导入android.content.Intent;
导入android.media.RingtoneManager;
导入android.net.Uri;
导入android.os.Bundle;
导入android.provider.MediaStore;
导入android.view.View;
导入android.widget.Button;
public class MainActivity extends Activity {
Button b;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
b =(Button)findViewById(R.id.button2);
b.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0){
Intent intent1 = new Intent();
intent1.setAction(Intent.ACTION_GET_CONTENT);
intent1.setType(audio / *);
startActivityForResult(
Intent.createChooser(intent1,选择声音文件), 6);
}
});
$ b @Override
protected void onActivityResult(int requestCode,int resultCode,Intent data){
if(resultCode == RESULT_OK&&&requestCode == 6){
Uri i = data.getData(); // getData
String s = i.getPath(); // getPath
文件k = new File(s); //设置文件路径
if(s!= null){// file.exists
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA,k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE,ring);
values.put(MediaStore.MediaColumns.MIME_TYPE,audio / mp3);
values.put(MediaStore.MediaColumns.SIZE,k.length());
values.put(MediaStore.Audio.Media.ARTIST,R.string.app_name);
values.put(MediaStore.Audio.Media.IS_RINGTONE,true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION,true);
values.put(MediaStore.Audio.Media.IS_ALARM,true);
values.put(MediaStore.Audio.Media.IS_MUSIC,false);
Uri uri = MediaStore.Audio.Media.getContentUriForPath(k
.getAbsolutePath());
getContentResolver()。delete(
uri,
MediaStore.MediaColumns.DATA += \
+ k.getAbsolutePath()+\,null );
Uri newUri = getContentResolver()。insert(uri,values);
尝试{
RingtoneManager.setActualDefaultRingtoneUri(
MainActivity.this,RingtoneManager.TYPE_RINGTONE,
newUri);
} catch(Throwable t){
}
}
}
}
}
$ b 最后,在你的 AndroidManifest.xml
中添加这些权限非常重要。不要添加权限来写外部存储你的应用程序将崩溃像我的.. xD
您需要:
< uses-permission android:name =android.permission.WRITE_SETTINGS/>
< uses-permission android:name =android.permission.WRITE_EXTERNAL_STORAGE/>
< uses-permission android:name =android.permission.CHANGE_CONFIGURATION/>
< uses-permission android:name =android.permission.MODIFY_AUDIO_SETTINGS/>
您可以在Google Play上试用我的应用:
..我的问题现在解决了!我希望我解决了你的问题:))
I am developing an application: I am trying to set an audio file as a ringtone.
I saw many posts, but no one actually helped me, so I decided to ask this question.
I use this code when the Button is clicked:
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("audio/*");
startActivityForResult(Intent.createChooser(intent, "Choose Sound File"), Audio);
}
in onActivityResult
I am trying to get the file path and then set the audio file as ringtone using the code below:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == Audio && requestCode==RESULT_OK) {
Uri s1 = data.getData();
String s = s1.getPath();
if(s!=null){
try {
k = new File(new URI(s)); //(File k;)
} catch (URISyntaxException e) {
e.printStackTrace();
}
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "My Song title");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mpeg");
values.put(MediaStore.Audio.Media.ARTIST, "Some Artist");
values.put(MediaStore.Audio.Media.IS_RINGTONE, false);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
values.put(MediaStore.Audio.Media.IS_ALARM, false);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);
//Insert it into the database
Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
Uri newUri = getContentResolver().insert(uri, values);
RingtoneManager.setActualDefaultRingtoneUri(
MainActivity.this,
RingtoneManager.TYPE_RINGTONE,
newUri);
}
}
}
Unfortunately this code doesn't work.
I would appreciate your answers. Sorry for my bad English.
I saw many posts but anyone showed what i should actually have to do. So i decided to create this complete answer , in which i have the solution of my problem...
Here is my MainActivity.java
which i used
import java.io.File;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Button b;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
b = (Button) findViewById(R.id.button2);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent1 = new Intent();
intent1.setAction(Intent.ACTION_GET_CONTENT);
intent1.setType("audio/*");
startActivityForResult(
Intent.createChooser(intent1, "Choose Sound File"), 6);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && requestCode == 6) {
Uri i = data.getData(); // getData
String s = i.getPath(); // getPath
File k = new File(s); // set File from path
if (s != null) { // file.exists
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "ring");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.MediaColumns.SIZE, k.length());
values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
values.put(MediaStore.Audio.Media.IS_ALARM, true);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);
Uri uri = MediaStore.Audio.Media.getContentUriForPath(k
.getAbsolutePath());
getContentResolver().delete(
uri,
MediaStore.MediaColumns.DATA + "=\""
+ k.getAbsolutePath() + "\"", null);
Uri newUri = getContentResolver().insert(uri, values);
try {
RingtoneManager.setActualDefaultRingtoneUri(
MainActivity.this, RingtoneManager.TYPE_RINGTONE,
newUri);
} catch (Throwable t) {
}
}
}
}
}
Lastly, its really important to add those permissions in your AndroidManifest.xml
for example if you don't add the permission to write external storage your app will crash like mine.. xD
What you need:
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
You can try my app on Google Play : BackAtel Audio Manager
Hope that helps.... my problem is now solved!! i hope that i solved your problem too :))
这篇关于将选定的音频文件设置为铃声的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!