Set raw file as ringtone problem

All your problems with Audio, Video and Images.

Set raw file as ringtone problem

Postby s1nn0n » Thu Mar 11, 2010 6:18 pm

Hi all,

I am using the following code trying to pull an .mp3 or .ogg file from my raw directory and saving it to the sd card and activating it as a ringtone. Although, it keeps failing for some reason or another and doesnt actually copy the new file to the sdcard.

Any ideas please?

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.   public boolean saveas(int ressound){
  2.  
  3.          byte[] buffer=null;
  4.  
  5.          InputStream fIn = getBaseContext().getResources().openRawResource(ressound);
  6.  
  7.          int size=0;
  8.  
  9.  
  10.  
  11.          try {
  12.  
  13.           size = fIn.available();
  14.  
  15.           buffer = new byte[size];
  16.  
  17.           fIn.read(buffer);
  18.  
  19.           fIn.close();
  20.  
  21.          } catch (IOException e) {
  22.  
  23.           // TODO Auto-generated catch block
  24.  
  25.           return false;
  26.  
  27.          }
  28.  
  29.  
  30.  
  31.          String path="/sdcard/media/audio/ringtones/";
  32.  
  33.          String filename="priceringtone"+".ogg";
  34.  
  35.  
  36.  
  37.          boolean exists = (new File(path)).exists();
  38.  
  39.          if (!exists){new File(path).mkdirs();}
  40.  
  41.  
  42.  
  43.          FileOutputStream save;
  44.  
  45.          try {
  46.  
  47.           save = new FileOutputStream(path+filename);
  48.  
  49.           save.write(buffer);
  50.  
  51.           save.flush();
  52.  
  53.           save.close();
  54.  
  55.          } catch (FileNotFoundException e) {
  56.  
  57.           // TODO Auto-generated catch block
  58.  
  59.           return false;
  60.  
  61.          } catch (IOException e) {
  62.  
  63.           // TODO Auto-generated catch block
  64.  
  65.           return false;
  66.  
  67.          }    
  68.  
  69.  
  70.  
  71.          sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename)));
  72.  
  73.  
  74.  
  75.          File k = new File(path, filename);
  76.  
  77.  
  78.  
  79.          ContentValues values = new ContentValues();
  80.  
  81.          values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
  82.  
  83.          values.put(MediaStore.MediaColumns.TITLE, "exampletitle");
  84.  
  85.          values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");
  86.  
  87.          values.put(MediaStore.Audio.Media.ARTIST, "cssounds ");
  88.  
  89.          values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
  90.  
  91.          values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
  92.  
  93.          values.put(MediaStore.Audio.Media.IS_ALARM, true);
  94.  
  95.          values.put(MediaStore.Audio.Media.IS_MUSIC, false);
  96.  
  97.  
  98.  
  99.          //Insert it into the database
  100.  
  101.          this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values);
  102.  
  103.  
  104.  
  105.          return true;
  106.  
  107.         }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4
s1nn0n
Freshman
Freshman
 
Posts: 4
Joined: Wed Aug 26, 2009 10:44 pm

Top

Postby ldrolez » Wed Mar 24, 2010 2:40 pm

I've tried you "copy to SD" code and it works (in the emulator)

I've tried with a small raw resource (63kb), maybe your resource is too big ?
The Android Open Source ressources and software database.: http://www.aopensource.com/
http://twitter.com/ldrlz
ldrolez
Junior Developer
Junior Developer
 
Posts: 14
Joined: Mon Mar 22, 2010 9:32 pm

Postby Th3sandm4n » Mon Apr 19, 2010 5:25 am

Are you getting any exceptions or errors? I'm guessing it is probably you not having

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />

In your manifest.xml

I'm not sure if the WRITE_EXTERNAL_STORAGE is redundant, but I just know its for 1.6+
Th3sandm4n
Freshman
Freshman
 
Posts: 8
Joined: Wed Mar 10, 2010 8:53 am

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 12 guests