Search code examples
androidandroid-fileringtone

Created file for ringtone doesn't have any sound


I made my app to get file from Raw folder and set that file as Ringtone. But there is a problem, the file is created and set as ringtone: http://prntscr.com/2so80e But file does not have any sound, and idk why I am guessing by default my device is playing another ringtone. Here is my code:

case 64:        
    String path = "android.resource://" + getPackageName() + "/"+R.raw.fusrodah;
    File k= new File(path);
    Log.i("OUTPUT", path);

    ContentValues values = new ContentValues();
    values.put(MediaStore.MediaColumns.DATA, k .getPath());
    values.put(MediaStore.MediaColumns.TITLE, "Fusrodah File");
    values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");
    values.put(MediaStore.Audio.Media.ARTIST, "Testing");
    values.put(MediaStore.MediaColumns.SIZE, 215454);
    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);

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

    break;

What Am I doing wrong? Is there something that I am missing? I have all permissions, file is created but doesn't have any sound.


Solution

  • It looks like you should copy your file to SD-card firstly, then use this copy as ringtone. Here full code sample (I have file "kalimba.mp3" in my assets):

    private int size;
    
    private static final int BUFFER_LEN = 1024;
    
    private void copyFile(AssetManager assetManager, String fileName, File out) throws FileNotFoundException, IOException {
        size = 0;
        FileOutputStream fos = new FileOutputStream(out);
        InputStream is = assetManager.open(fileName);       
        int read = 0;
        byte[] buffer = new byte[BUFFER_LEN];
         while ((read = is.read(buffer, 0, BUFFER_LEN)) >= 0) {
                fos.write(buffer, 0, read);
                size += read;
          }
        fos.flush();    
        fos.close();
        is.close();
    }
    
    @Override
    public void onClick(View arg0) {        
        AssetManager assetManager = getAssets();
    
        File file = new File(Environment.getExternalStorageDirectory(),
                "/myRingtonFolder/Audio/");
        if (!file.exists()) {
            file.mkdirs();
        }
    
        String path = Environment.getExternalStorageDirectory()
                .getAbsolutePath() + "/myRingtonFolder/Audio/";
    
        File out = new File(path + "/", "kalimba.mp3");     
        if(!out.exists()){
            try {
                copyFile(assetManager, "kalimba.mp3", out);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }           
    
        ContentValues values = new ContentValues();
        values.put(MediaStore.MediaColumns.DATA, out.getAbsolutePath());
        values.put(MediaStore.MediaColumns.TITLE, "name");
        values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
        values.put(MediaStore.MediaColumns.SIZE, out.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(out.getAbsolutePath());
        ContentResolver mCr = getContentResolver();
        Uri newUri = mCr.insert(uri, values);
    
        try {
            RingtoneManager.setActualDefaultRingtoneUri(this,    RingtoneManager.TYPE_RINGTONE, newUri);
            Settings.System.putString(mCr, Settings.System.RINGTONE,
                    newUri.toString());
        } 
        catch (Throwable t) 
        {
            //TODO Handle exception
        }
    
    }