Wednesday, September 23, 2015

// //

Set raw resource file as ringtone in android programmatically

Hi Guys! Today I'm going to show you how to set the ringtone from the resource folder. :)
Now don't forget to comment if you have encounter problems.

Note: You have to provide your own mp3 tone.

Okay, lets get started.

The MainActivity.java




import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

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.os.Environment;
import android.provider.MediaStore;

public class MainActivity extends Activity {
 String exStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath();
 String path=(exStoragePath +"/media/alarms/"); 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  saveas(RingtoneManager.TYPE_RINGTONE);
 }

 // In this method, we need to copy the mp3 file to the sd card location from
 // where android picks up ringtone files
 // After copying, we make the mp3 as current ringtone
 public boolean saveas(int type) {
  byte[] buffer = null;
  InputStream fIn = getBaseContext().getResources().openRawResource(
    R.raw.sample);
  int size = 0;

  try {
   size = fIn.available();
   buffer = new byte[size];
   fIn.read(buffer);
   fIn.close();
  } catch (IOException e) {
   return false;
  }

  
  String filename = "GO";

  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) {
   return false;
  } catch (IOException e) {
   return false;
  }


    sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+path+filename+".mp3"
              + Environment.getExternalStorageDirectory()))); 

  
 

  File k = new File(path, filename);

  ContentValues values = new ContentValues(4);
  long current = System.currentTimeMillis();
   values.put(MediaStore.MediaColumns.DATA, path + filename  );
   values.put(MediaStore.MediaColumns.TITLE,  filename );
  values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current / 1000));
  values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/3gpp");
  
  //new
   values.put(MediaStore.Audio.Media.ARTIST, "cssounds ");
    values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
    values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
    values.put(MediaStore.Audio.Media.IS_ALARM, true);
    values.put(MediaStore.Audio.Media.IS_MUSIC, false);  

  // Insert it into the database
  this.getContentResolver()
    .insert(MediaStore.Audio.Media.getContentUriForPath(k
      .getAbsolutePath()), values);

  return true;
 }
}
//Thus our ringtone was set successfuly

My ringtone filename is GO.

This is the activity_main.xml








And in your AndroidManifest.xml

  
    
      

HAPPY CODING!