Search code examples
javaandroidstringandroid-intentringtone

Using Intents to pass data for ringtones


So far I've been making applications with set as Ringtone feature by creating 1 activity for 1 file. It was bad because with apps with more than 20 ringtones I would've needed 20 activities which would affect app size and performance. Then I found that there is a way to do that with only 1 activity and layout, passing data with Intents. Now I have pretty good idea how that works except one thing that bothers me. That is how do I define strings. I need 1 string for name and 1 for file path

My code:

Boolean success = false;
                    rsound = new File(rpath, "Slow tone.mp3");rpath.mkdirs(); //Copied file name
                    if (!rsound.exists()) {




                        try {
                            InputStream in = getResources().openRawResource(R.raw.s8slowtone); //path for file 
                            FileOutputStream out = new FileOutputStream(rsound.getPath());
                            byte[] buff = new byte[1024];
                            int read = 0;

                            try {
                                while ((read = in.read(buff)) > 0) {
                                    out.write(buff, 0, read);
                                }
                            } finally {
                                in.close();

                                out.close();
                            }
                        } catch (Exception e) {
                            success = false;

                        }
                    } else {
                        success = true;
                        setRingtone();

                    }

                    if (!success) { 
                       setRingtone();


                    }
                }

                private void setRingtone() {
                    ContentValues values = new ContentValues();
                       values.put(MediaStore.MediaColumns.DATA, rsound.getAbsolutePath());
                       values.put(MediaStore.MediaColumns.TITLE, "Slow tone"); //Ringtone name
                       values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
                       values.put(MediaStore.Audio.Media.ARTIST, " ");
                       values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
                       values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
                       values.put(MediaStore.Audio.Media.IS_ALARM, false);
                       values.put(MediaStore.Audio.Media.IS_MUSIC, true);

                       Uri uri = MediaStore.Audio.Media.getContentUriForPath(rsound.getAbsolutePath());
                       getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + rsound.getAbsolutePath() + "\"",
                               null);
                       Uri newUri = getContentResolver().insert(uri, values);

                       RingtoneManager.setActualDefaultRingtoneUri(
                               S15.this, RingtoneManager.TYPE_RINGTONE,
                               newUri);
                       Toast.makeText(getApplicationContext(), "Ringtone set successfully",
                               Toast.LENGTH_SHORT).show();

So How do I do this? How do I define string for each file and how to pass them?

Since question is unclear for some members I will make it simpler I don't have idea how should I write strings so when I start RingtoneManager Activity using Intent, I pass data from strings. So How should I write my code to pass this

File name "Slow tone.mp3"

File path: R.raw.s8slowtone)

Ringtone name "Slow tone"


Solution

  •  Intent f27=new Intent(context, RMpro.class);
             if (f27 != null){
             f27.putExtra("FileName", "Horn!"); //Copied file name
             int res = R.raw.s28horn; // Path to File in App ressources
             f27.putExtra("FilePath", res); //Passing path with intent
             f27.putExtra("RingName", "Horn.mp3"); // Ring name
             ((Activity)context).startActivity(f27);
             }
    

    And then in Ringtone Manager, in my case RMpro

     final int FPATH=i.getExtras().getInt("FilePath");
           final  String RNAME = getIntent().getStringExtra("RingName").trim();
           final  String FNAME = getIntent().getStringExtra("FileName").trim();
    

    And then just:

    rsound = new File(rpath, FNAME);rpath.mkdirs();
    
    InputStream in = getResources().openRawResource(FPATH);
    
    values.put(MediaStore.MediaColumns.TITLE, RNAME);