使用服务器的铃声设置默认铃声

时间:2015-05-18 21:08:13

标签: android

我试图通过使用我服务器上的铃声来设置默认铃声。首先我尝试使用此代码将我的铃声从url下载到SD卡:

distBetween

之后,我尝试使用以下代码从SD卡中设置铃声:

private void startDownload() {
        String url = music;
        new DownloadFileAsync().execute(url);
    }
    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DIALOG_DOWNLOAD_PROGRESS:
            mProgressDialog = new ProgressDialog(this);
            mProgressDialog.setMessage("Downloading file..");
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            mProgressDialog.setCancelable(false);
            mProgressDialog.show();
            return mProgressDialog;
        default:
            return null;
        }
    }

class DownloadFileAsync extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        showDialog(DIALOG_DOWNLOAD_PROGRESS);
    }
    @Override
    protected String doInBackground(String... aurl) {
        int count;

    try {
    URL url = new URL(aurl[0]);
    URLConnection conexion = url.openConnection();
    conexion.connect();
    int lenghtOfFile = conexion.getContentLength();
    Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);

    InputStream input = new BufferedInputStream(url.openStream());
    File sdCard = Environment.getExternalStorageDirectory();
    File dir = new File (sdCard.getAbsolutePath() + "/myfoldername/dir3");
    dir.mkdirs();
    File file = new File(dir, myringtonename + ".mp3");
    FileOutputStream output = new FileOutputStream(file);
    byte data[] = new byte[1024];
    long total = 0;
        while ((count = input.read(data)) != -1) {
            total += count;
            publishProgress(""+(int)((total*100)/lenghtOfFile));
            output.write(data, 0, count);
        }
        output.flush();
        output.close();
        input.close();
    } catch (Exception e) {}
    return null;
    }
    protected void onProgressUpdate(String... progress) {
         Log.d("ANDRO_ASYNC",progress[0]);
         mProgressDialog.setProgress(Integer.parseInt(progress[0]));
    }
    @Override
    protected void onPostExecute(String unused) {
        dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
    }
}

下载代码是完美的,但铃声将保存在内部存储而不是SD卡。 设置铃声代码不起作用,我在logcat中看到这个笔记:

    private void setrington() {
            String path = getFilesDir()+"/sdcard/myfoldername/dir3/";
            File k = new File(path, myringtonename + ".mp3"); // path is a file to /sdcard/media/ringtone
            ContentValues values = new ContentValues();
            values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
            values.put(MediaStore.MediaColumns.TITLE, "My Song title");
            values.put(MediaStore.MediaColumns.SIZE, 215454);
            values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
            values.put(MediaStore.Audio.Media.ARTIST, "Madonna");
            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);
            Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
Uri newUri = Etelaat.this.getContentResolver().insert(uri, values);
RingtoneManager.setActualDefaultRingtoneUri(getApplicationContext(),RingtoneManager.TYPE_RINGTONE,newUri);

        }

我也试过这个:

05-19 01:26:23.841: V/Provider/Settings(3485):  from settings cache , name = sound_effects_enabled , value = 0
05-19 01:26:23.848: D/ActivityThread(3485): hoder:android.app.IActivityManager$ContentProviderHolder@42ce86c0,provider,holder.Provider:android.content.ContentProviderProxy@42ce9098
05-19 01:26:23.979: D/Provider/Settings(3485): put string name = ringtone , value = content://media/internal/audio/media/142 userHandle = 0
05-19 01:26:24.031: I/RingtoneManager(3485): Set actual default ringtone uri= content://media/internal/audio/media/142

它也不起作用:

File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + "/myfoldername/dir3");
dir.mkdirs();
File file = new File(dir, myringtonename + ".mp3");

那我的代码怎么出错呢?

0 个答案:

没有答案
相关问题