2011-07-27 39 views
0

編輯:設置鈴聲類沒有做任何事情

整個SetRingtone.java -

public class SetRingtone extends Activity{ 

String TAG = "CFFS"; // Class var for logging - identifies the app in the logcat 

public boolean saveas(int ressound){ 
     byte[] buffer=null; 
     InputStream fIn = getBaseContext().getResources().openRawResource(ressound); 
     int size=0; 

     try { 
     size = fIn.available(); 
     buffer = new byte[size]; 
     fIn.read(buffer); 
     fIn.close(); 
     } catch (IOException e) { 
     // TODO Auto-generated catch block 
     return false; 
     } 

     String path = Environment.getExternalStorageDirectory().getPath() + "/media/ringtone/ringtone.mp3"; 
     String filename="College Football Fight Song"+".mp3"; 


     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) { 
     // TODO Auto-generated catch block 
     return false; 
     } catch (IOException e) { 
     // TODO Auto-generated catch block 
     return false; 
     }  

     sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename))); 


    File k = new File(path, filename); 

    ContentValues values = new ContentValues(); 
    values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath()); 
    values.put(MediaStore.MediaColumns.TITLE, "College Football Fight Song"); 
    values.put(MediaStore.MediaColumns.SIZE, 215454); 
    values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3"); 
    values.put(MediaStore.Audio.Media.ARTIST, ""); 
    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); 

    //Insert it into the database 

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

    return false; 
} 

的Java,我想設置的鈴聲 -

private OnLongClickListener onLongImageClick = new OnLongClickListener() { 
    @Override 
    public boolean onLongClick(View v) { 
     if (v.getId() == R.id.boston_college_imageview) { 
      SetRingtone(R.raw.acc_boston_college); 
     } 
     return false; 
     } 
}; 

,並在那裏我將它傳遞off to SetRingtone.java -

private void SetRingtone(int soundID) { 
    Intent otherIntent = new Intent(); 
    otherIntent.setClassName("com.carboni.fightsongs", "com.carboni.fightsongs.SetRingtone"); 
    otherIntent.putExtra("com.carboni.fightsongs.FILE_RES_ID", soundID); 
    startActivity(otherIntent); 
} 
+0

調試一行行的代碼,看看是否有異常的地方。如果問題出現在try-catch中的代碼中,則錯誤不會顯示。 –

+0

好的,我會試試。但是從你能看到的情況來看,有什麼看起來不對的? – ericcarboni

+0

您可能需要填寫contentresolver的所有屬性的值。您可能必須執行'context.getContentResolver()'而不是'getContentResolver()'。另外,也許你的文件路徑不正確。看看[這個](http://stackoverflow.com/questions/1271777/how-to-set-ringtone-in-android-from-my-activity)和[this](http://coderzheaven.com/2010/10/how-to-set-ringtone-in-android /)獲得更多幫助。 –

回答

0

This i S中長按應該是什麼樣子與ImageView的:

imageView1.setOnLongClickListener(new ImageView.OnLongClickListener() 
{ 
    public boolean onLongClick(View v) 
    { 
     if (v.getId() == R.id.boston_college_imageview) 
     { 
      SetRingtone(R.raw.acc_boston_college); 
     } 
     return false; 
    } 
}); 

UPDATE:

String path = Environment.getExternalStorageDirectory().getPath() + "/media/ringtone"; 
String filename="College Football Fight Song.mp3"; 

boolean exists = (new File(path)).exists(); 
if (!exists){new File(path).mkdirs();} 

FileOutputStream save; 
try 
{ 
    File file = new File(path, filename); 
    save = new FileOutputStream(file); 
    save.write(buffer); 
    save.flush(); 
    save.close(); 
} 
catch (FileNotFoundException e) 
{ 
    return false; 
} 
catch (IOException e) 
{ 
    return false; 
} 
+0

它似乎仍然沒有工作。我嘗試了很多不同的東西,現在甚至都沒有改變鈴聲。什麼是鈴聲應該是再次的路徑? – ericcarboni

+0

我相信它應該看起來像這樣:'//String/= ringtone/ringtone/ringtone .mp3「;' –

+0

爲什麼字符串'path'聲明瞭兩次? – ericcarboni