设为铃声Android需要双击

时间:2014-02-27 10:58:10

标签: android methods ringtone

我收到了这段代码: `

protected void setRingtone() {
        // TODO Auto-generated method stub
        Boolean success = false;
        sound = new File(folder, "Ilidan.mp3");
        if (!sound.exists()) {
            Log.i(TAG, "Ilidan " + folder.toString());
            try {
                InputStream in = getResources().openRawResource(R.raw.ilidan);
                FileOutputStream out = new FileOutputStream(sound.getPath());
                byte[] buff = new byte[1024];
                int read = 0;

            try {
                while ((read = in.read(buff)) > 0) {
                    out.write(buff, 0, read);
                }
            } finally {
                in.close();

                out.close();
            }
        } catch (Exception e) {
            success = false;
            Log.i(TAG, "Ne da radi.");
        }
    } else {
        success = true;
        Log.i(TAG, "VEc postoji.");
    }

    if (!success) {
        onSetRingtoneError("There was issue writting file.");
    } else {
        setSettingsRingtone();
    }
}

private void onSetRingtoneError(String string) {
    // TODO Auto-generated method stub
    Toast.makeText(MainActivity.this, "Nije Radilo ", 5000).show();

}

private void setSettingsRingtone() {
    // TODO Auto-generated method stub
    ContentValues values = new ContentValues();
    values.put(MediaStore.MediaColumns.DATA, sound.getAbsolutePath());
    values.put(MediaStore.MediaColumns.TITLE, "Alert");
    values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
    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(sound.getAbsolutePath());
    getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + sound.getAbsolutePath() + "\"",
            null);
    Uri newUri = getContentResolver().insert(uri, values);

    RingtoneManager.setActualDefaultRingtoneUri(
            MainActivity.this, RingtoneManager.TYPE_RINGTONE,
            newUri);


}

{

}

但这是发生的事情。当我第一次点击按钮时,它只创建文件,第二次点击它时设置为铃声。 我究竟做错了什么?如何让我的应用程序复制文件并在1次点击中将其设置为铃声?

2 个答案:

答案 0 :(得分:2)

创建文件if(!sound.exists())后,您应将成功设置为true

success = true;

在你的行动中,你声明了afile,然后你测试了它(如果存在与否),如果(!存在),你创建文件,但是你忘记将成功设置为true,

//your code
finally {
                in.close();
                 out.close();
                 success = true;
            }

答案 1 :(得分:1)

   protected void setRingtone() {
            // TODO Auto-generated method stub
            Boolean success = false;
            sound = new File(folder, "Ilidan.mp3");
            if (!sound.exists()) {
                Log.i(TAG, "Ilidan " + folder.toString());
                try {
                    InputStream in = getResources().openRawResource(R.raw.ilidan);
                    FileOutputStream out = new FileOutputStream(sound.getPath());
                    byte[] buff = new byte[1024];
                    int read = 0;

                try {
                    while ((read = in.read(buff)) > 0) {
                        out.write(buff, 0, read);
                    }
                } finally {
                    in.close();
                     out.close();
                     success = true;
                }
            } catch (Exception e) {
                success = false;
                Log.i(TAG, "Ne da radi.");
            }
        } else {
            success = true;
            Log.i(TAG, "VEc postoji.");
        }

        if (!success) {
            onSetRingtoneError("There was issue writting file.");
        } else {
            setSettingsRingtone();
        }
    }

    private void onSetRingtoneError(String string) {
        // TODO Auto-generated method stub
        Toast.makeText(MainActivity.this, "Nije Radilo ", 5000).show();

    }

    private void setSettingsRingtone() {
        // TODO Auto-generated method stub
        ContentValues values = new ContentValues();
        values.put(MediaStore.MediaColumns.DATA, sound.getAbsolutePath());
        values.put(MediaStore.MediaColumns.TITLE, "Alert");
        values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
        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(sound.getAbsolutePath());
        getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + sound.getAbsolutePath() + "\"",
                null);
        Uri newUri = getContentResolver().insert(uri, values);

        RingtoneManager.setActualDefaultRingtoneUri(
                MainActivity.this, RingtoneManager.TYPE_RINGTONE,
                newUri);


    }

    {

    }

我只是改变你的最终喜欢

 finally {
                in.close();
                 out.close();
                 success = true;
            }

coz第一次只创建文件夹,成功布尔值为false然后下次是真的所以只需更新你的终于