Set raw resource as ringtone in Android

In my android application, I want to set audio file from my raw folder as a ringtone. For that i wrote below code, but its not working.

Please help me to solve this issue. Thank you.

Code :

String name =best_song_ever.mp3;
File newSoundFile = new File("/sdcard/media/ringtone",
                        "myringtone.mp3");
                Uri mUri = Uri.parse("android.resource://"
                        + context.getPackageName() + "/raw/" + name);
                ContentResolver mCr = context.getContentResolver();
                AssetFileDescriptor soundFile;
                try {
                    soundFile = mCr.openAssetFileDescriptor(mUri, "r");
                } catch (FileNotFoundException e) {
                    soundFile = null;
                }

            try {
                byte[] readData = new byte[1024];
                FileInputStream fis = soundFile.createInputStream();
                FileOutputStream fos = new FileOutputStream(newSoundFile);
                int i = fis.read(readData);

                while (i != -1) {
                    fos.write(readData, 0, i);
                    i = fis.read(readData);
                }

                fos.close();
            } catch (IOException io) {
            }
            ContentValues values = new ContentValues();
            values.put(MediaStore.MediaColumns.DATA,
                    newSoundFile.getAbsolutePath());
            values.put(MediaStore.MediaColumns.TITLE, "my ringtone");
            values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/oog");
            values.put(MediaStore.MediaColumns.SIZE, newSoundFile.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(newSoundFile.getAbsolutePath());
            Uri newUri = mCr.insert(uri, values);

            try {
                RingtoneManager.setActualDefaultRingtoneUri(context,
                        RingtoneManager.TYPE_RINGTONE, newUri);
            } catch (Throwable t) {

            }

            Toast.makeText(context, name + " is set as ringtone.",
                    Toast.LENGTH_LONG).show();
        }

Answers


The following code solved my problem :

String name = "your_raw_audio_name";

            File file = new File(Environment.getExternalStorageDirectory(),
                    "/myRingtonFolder/Audio/");
            if (!file.exists()) {
                file.mkdirs();
            }

            String path = Environment.getExternalStorageDirectory()
                    .getAbsolutePath() + "/myRingtonFolder/Audio/";

            File f = new File(path + "/", name + ".mp3");

            Uri mUri = Uri.parse("android.resource://"
                    + context.getPackageName() + "/raw/" + name);
            ContentResolver mCr = context.getContentResolver();
            AssetFileDescriptor soundFile;
            try {
                soundFile = mCr.openAssetFileDescriptor(mUri, "r");
            } catch (FileNotFoundException e) {
                soundFile = null;
            }

            try {
                byte[] readData = new byte[1024];
                FileInputStream fis = soundFile.createInputStream();
                FileOutputStream fos = new FileOutputStream(f);
                int i = fis.read(readData);

                while (i != -1) {
                    fos.write(readData, 0, i);
                    i = fis.read(readData);
                }

                fos.close();
            } catch (IOException io) {
            }
            ContentValues values = new ContentValues();
            values.put(MediaStore.MediaColumns.DATA, f.getAbsolutePath());
            values.put(MediaStore.MediaColumns.TITLE, name);
            values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
            values.put(MediaStore.MediaColumns.SIZE, f.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(f
                    .getAbsolutePath());
            Uri newUri = mCr.insert(uri, values);

            try {
                RingtoneManager.setActualDefaultRingtoneUri(context,
                        RingtoneManager.TYPE_RINGTONE, newUri);
                Settings.System.putString(mCr, Settings.System.RINGTONE,
                        newUri.toString());
            } catch (Throwable t) {

            }

Need Your Help

Processingjs not showing up in Firefox 3.6

javascript html5 processing firefox3.6

I'm attempting to develop a new website using the Processsingjs library. It works fine in Chrome, but the canvas is showing up blank in Firefox.

What is the difference between setting the path for Java in user variables and system variables?

java windows environment-variables

I'm installing Java on my windows. I'd like to know the difference between setting the path for Java in user variables and setting it in System variables?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.