0

EDIT:

The entire SetRingtone.java --

public class SetRingtone extends Activity{

String TAG = "CFFS"; // Class var for logging - identifies the app in the logcat

public boolean saveas(int ressound){  
      byte[] buffer=null;  
      InputStream fIn = getBaseContext().getResources().openRawResource(ressound);  
      int size=0;  

      try {  
       size = fIn.available();  
       buffer = new byte[size];  
       fIn.read(buffer);
       fIn.close();  
      } catch (IOException e) {  
       // TODO Auto-generated catch block  
       return false;  
      }  

      String path = Environment.getExternalStorageDirectory().getPath() + "/media/ringtone/ringtone.mp3";
      String filename="College Football Fight Song"+".mp3";  


      boolean exists = (new File(path)).exists();  
      if (!exists){new File(path).mkdirs();}  

      FileOutputStream save;  
      try {  
       save = new FileOutputStream(path+filename);  
       save.write(buffer);  
       save.flush();  
       save.close();  
      } catch (FileNotFoundException e) {  
       // TODO Auto-generated catch block  
       return false;  
      } catch (IOException e) {  
       // TODO Auto-generated catch block  
       return false;  
      }      

      sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename)));  


     File k = new File(path, filename);  

     ContentValues values = new ContentValues();  
     values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
     values.put(MediaStore.MediaColumns.TITLE, "College Football Fight Song");
     values.put(MediaStore.MediaColumns.SIZE, 215454);
     values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
     values.put(MediaStore.Audio.Media.ARTIST, "");
     values.put(MediaStore.Audio.Media.DURATION, 230);
     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, false);

     //Insert it into the database  

     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(
             SetRingtone.this,
       RingtoneManager.TYPE_RINGTONE,
       newUri
     );

    return false;
}

Java where I want to set ringtone --

private OnLongClickListener onLongImageClick = new OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        if (v.getId() == R.id.boston_college_imageview) {
            SetRingtone(R.raw.acc_boston_college);
        }
        return false;
        }
};

and where I pass it off to SetRingtone.java --

private void SetRingtone(int soundID) {
    Intent otherIntent = new Intent();
    otherIntent.setClassName("com.carboni.fightsongs", "com.carboni.fightsongs.SetRingtone");
    otherIntent.putExtra("com.carboni.fightsongs.FILE_RES_ID", soundID);
    startActivity(otherIntent);
}
ericcarboni
  • 88
  • 1
  • 13
  • Debug the code line by line and see if there is an exception somewhere. The error doesn't show if the problem is with the code that is in the try-catch. – A. Abiri Jul 27 '11 at 19:46
  • Okay, I will try that. But from what you can see, is there anything that looks wrong? – ericcarboni Jul 27 '11 at 19:59
  • You might have to fill in the values for all of the properties of contentresolver. You might have to do `context.getContentResolver()` instead of just `getContentResolver()`. Also, maybe your filepath is incorrect. Take a look at [this](http://stackoverflow.com/questions/1271777/how-to-set-ringtone-in-android-from-my-activity) and [this](http://coderzheaven.com/2010/10/how-to-set-ringtone-in-android/) for more help. – A. Abiri Jul 27 '11 at 21:12
  • Still not working for me... Am I passing off the onLongClick to the java class correctly? I feel like when I long click on the imageview, the SetRingtone class isn't getting the media file, because when I call myself, it is a blank ringtone. – ericcarboni Jul 27 '11 at 23:57
  • I am not sure what type of view you are using for the long click, so I am going to show you an example of how the long click should look like with the type `View` in an answer since I don't have space in the comment. – A. Abiri Jul 28 '11 at 00:20
  • I just realized you said you are using an imageview in your previous comment, so I changed the answer to work with an imageview. – A. Abiri Jul 28 '11 at 00:29

1 Answers1

0

This is what the long click should look like with an ImageView:

imageView1.setOnLongClickListener(new ImageView.OnLongClickListener()
{
    public boolean onLongClick(View v)
    {
        if (v.getId() == R.id.boston_college_imageview)
        {
            SetRingtone(R.raw.acc_boston_college);
        }
        return false;
    }
});

UPDATE:

String path = Environment.getExternalStorageDirectory().getPath() + "/media/ringtone";
String filename="College Football Fight Song.mp3";  

boolean exists = (new File(path)).exists();  
if (!exists){new File(path).mkdirs();}  

FileOutputStream save;
try
{
    File file = new File(path, filename);
    save = new FileOutputStream(file);
    save.write(buffer);
    save.flush();
    save.close();
}
catch (FileNotFoundException e)
{
    return false;  
}
catch (IOException e)
{
    return false;  
}
A. Abiri
  • 10,750
  • 4
  • 30
  • 31
  • It still doesn't seem to be working. I've tried a lot of different things, and now it isn't even changing the ringtone at all. What is the path for the ringtone supposed to be again? – ericcarboni Jul 28 '11 at 04:17
  • I believe it should look like this: `// String path = "/sdcard/media/ringtone/ringtone.mp3;` `String path = Environment.getExternalStorageDirectory().getPath() + "/media/ringtone/ringtone.mp3";` – A. Abiri Jul 28 '11 at 17:49
  • Why is the string 'path' declared twice though? – ericcarboni Jul 28 '11 at 23:29
  • The first line was meant to be a comment to give an example how the string would look like in the string 'path' in the second line. The second line is how you should right the code and the first line is how the string would look like to the android device. Look at my update if you don't get what I mean. – A. Abiri Jul 29 '11 at 05:16
  • Am I just completely incapable of doing this? I cannot figure out what I'm doing wrong at all. I think I've tried everything. Could there be a problem with my phone, causing it not to work? – ericcarboni Jul 29 '11 at 18:08
  • I highly doubt that there is any problem with your phone. I'm pretty sure that there is something wrong with your path. In your `saveas()` function in the `SetRingtone`, you have start the variable path with Environment.getExternalStorageDirectory().getPath(). The device doesn't know what location you are talking about if you don't give the root directory. Also, make sure that the folder exists, if not, call file.mkdirs() so that it is created. – A. Abiri Jul 29 '11 at 20:27
  • I think your misunderstanding what I'm trying to say. It's suppose to be `String path = Environment.getExternalStorageDirectory().getPath() + "/media/ringtone/";`. – A. Abiri Jul 29 '11 at 21:07
  • I'm just going change a part of your code and put it up on my answer as an update. – A. Abiri Jul 29 '11 at 21:15
  • Ugh, I've now tried it on 2 devices, and still not working. I would like to know how long clicking the imageview goes to the SetRingtone class so that it will set the ringtone that I clicked on. I think that's the problem – ericcarboni Jul 29 '11 at 22:29
  • Your onlongClick listener should look like the one I have in my answer. The way you wrote it creates a long click listener for the entire activity, and not the imageView. Also, your SetRingtone class should have a constructor that calls the saveas() function when the class is called or else the saveas() function will never be called. – A. Abiri Jul 30 '11 at 03:12