2つの質問があります:
saveas(ressound);
android 4.0.3デバイスで(stealthcopter.comに触発されて)電話をかけると、着信音は着信音として適切にアクティブ化されますが、2.3.3デバイスでは、選択した着信音は無音になります。コードのどこが間違っていたのですか?私が知りたい2番目のことはファイルを削除する方法です。さらに重要なのは、メディアストアからそれを取り出す方法ですが、sdから削除すると、これは自動的に行われますか?
アップデート:
フォルダ全体とコンテンツを削除するコードを追加しました。正常に動作し、クリーンです。(下記参照)
ボタンを押したときにこのコードを呼び出してみてください。
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
ただし、メディアストアはクリーンアップされません。再起動すると機能します。開発ツール内でメディアスキャンも試しましたが、機能しません。一部のアプリを再起動する必要があるとのことですが、デバイス全体を再起動せずに、使用可能な着信音のリストを表示するサービスを再起動するにはどうすればよいですか?
変数:
String soundname;
int ressound;
int savetype;
String path="/sdcard/mysounds/";
savetype = RingtoneManager.TYPE_RINGTONE;
ressound = R.raw.sound1;
soundname = "sound1";
saveas(ressound);
//save as
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) {
return false;
}
String filename= soundname +".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) {
return false;
} catch (IOException e) {
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, soundname);
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.Audio.Media.ARTIST, "Elvis");
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, false);
//Insert it into the database
Uri newUri = this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values);
// set as ringtone
RingtoneManager.setActualDefaultRingtoneUri(this, savetype, newUri);
return true;
}
消去();
public void delete() {
File a = new File(path, "sound1.mp3");
a.delete();
File b = new File(path, "sound2.mp3");
b.delete();
}
deleteFiles(文字列パス);
public static void deleteFiles(String path) {
File file = new File(path);
if (file.exists()) {
String deleteCmd = "rm -r " + path;
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec(deleteCmd);
} catch (IOException e) { }
}
}