1st. I got a code to set as Ringtone. One thing I don't understand. I have 64 sound files in my application. Is there a way to get something like ID when I click on some button or I have to write whole code for all 64 files?
2nd. I have a listview in my app, and I want on long item click to set that file as ringtone. Now. Do I need to write whole code into case, or I can make another activity and then just set ringtone using that way? Something like this
case 1:
RingtoneManager.setActualDefaultRingtoneUri(
myActivity,
Activity name.TYPE_RINGTONE,
newUri
break;
Well that's about it, Is it possible to get ID from file, copy that file and set it as ringtone on long click, or I need to write code for all 64 files? Edit: If 1st question is not possible, how should I write code for all 64 files? Where to put them? And how can I define which file to be copied when I click on certain button. Or list view
My code is:
l.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String selectedName = ((ListView) view.findViewById(R.id.listView1)).getContext().toString();
AssetManager assetManager = getAssets();
File file = new File(Environment.getExternalStorageDirectory(),
"/myRingtonFolder/Audio/");
if (!file.exists()) {
file.mkdirs();
}
String path = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/MyRingtones/Audio/";
File out = new File(path + "/", selectedName);
if(!out.exists()){
copyFile(assetManager, "Yeah.mp3", out);
}
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, out.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "yeah");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.MediaColumns.SIZE, out.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, true);
Uri uri = MediaStore.Audio.Media.getContentUriForPath(out.getAbsolutePath());
ContentResolver mCr = getContentResolver();
Uri newUri = mCr.insert(uri, values);
try {
RingtoneManager.setActualDefaultRingtoneUri(
getApplicationContext(), RingtoneManager.TYPE_RINGTONE, newUri);
Settings.System.putString(mCr, Settings.System.RINGTONE,
newUri.toString());
}
catch (Throwable t)
{
//TODO Handle exception
}
switch (position){
case 0:
if(mp!=null)
{
mp.release();
mp=null;
}
MediaPlayer.create(MainActivity.this, R.raw.aint);
mp.start();
break;
case 1:
if(mp!=null)
{
mp.release();
mp=null;
}
mp = MediaPlayer.create(MainActivity.this,
R.raw.airh);
mp.start();
LOGCAT:
02-25 09:21:34.064: W/dalvikvm(10221): threadid=1: thread exiting with uncaught exception (group=0x41aac8b0)
02-25 09:21:34.074: E/AndroidRuntime(10221): FATAL EXCEPTION: main
02-25 09:21:34.074: E/AndroidRuntime(10221): java.lang.NullPointerException
02-25 09:21:34.074: E/AndroidRuntime(10221): at funnysounds.com.MainActivity$1.onItemClick(MainActivity.java:63)
02-25 09:21:34.074: E/AndroidRuntime(10221): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
02-25 09:21:34.074: E/AndroidRuntime(10221): at android.widget.AbsListView.performItemClick(AbsListView.java:1104)
02-25 09:21:34.074: E/AndroidRuntime(10221): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2792)
02-25 09:21:34.074: E/AndroidRuntime(10221): at android.widget.AbsListView$1.run(AbsListView.java:3468)
02-25 09:21:34.074: E/AndroidRuntime(10221): at android.os.Handler.handleCallback(Handler.java:730)
02-25 09:21:34.074: E/AndroidRuntime(10221): at android.os.Handler.dispatchMessage(Handler.java:92)
02-25 09:21:34.074: E/AndroidRuntime(10221): at android.os.Looper.loop(Looper.java:213)
02-25 09:21:34.074: E/AndroidRuntime(10221): at android.app.ActivityThread.main(ActivityThread.java:5225)
02-25 09:21:34.074: E/AndroidRuntime(10221): at java.lang.reflect.Method.invokeNative(Native Method)
02-25 09:21:34.074: E/AndroidRuntime(10221): at java.lang.reflect.Method.invoke(Method.java:525)
02-25 09:21:34.074: E/AndroidRuntime(10221): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
02-25 09:21:34.074: E/AndroidRuntime(10221): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
02-25 09:21:34.074: E/AndroidRuntime(10221): at dalvik.system.NativeStart.main(Native Method)
I would not use a switch but put a click listener :
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// selected item
String selectedName = ((TextView) view.findViewById(R.id.your_textView_item_id)).getText().toString();
AssetManager assetManager = getAssets();
File file = new File(Environment.getExternalStorageDirectory(),
"/myRingtonFolder/Audio/");
if (!file.exists()) {
file.mkdirs();
}
String path = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/MyRingtones/Audio/";
File out = new File(path + "/", selectedName);
if(!out.exists()){
try {
copyFile(assetManager, "Yeah.mp3", out);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, out.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "yeah");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.MediaColumns.SIZE, out.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, true);
Uri uri = MediaStore.Audio.Media.getContentUriForPath(out.getAbsolutePath());
ContentResolver mCr = getContentResolver();
Uri newUri = mCr.insert(uri, values);
try {
RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, newUri);
Settings.System.putString(mCr, Settings.System.RINGTONE,
newUri.toString());
}
catch (Throwable t)
{
//TODO Handle exception
}
}
});