我使用下面的代碼把audio 文件從res/raw文件夾中移動到 SD card,當我執行這段代碼時,文件沒有移動。哪一行出錯呢?
MoveAudio.java
public class MoveAudioextends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button a = (Button) findViewById(R.id.Button01);
a.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
byte[] buffer = null;
InputStream fIn = getBaseContext().getResources()
.openRawResource(R.raw.song);
int size = 0;
System.out.println("<<<<<<<SIZE>>>>>>>>>>>>>>>>>>>>" + fIn);
try {
size = fIn.available();
System.out
.println("<<<<<<<SIZE>>>>>>>>>>>>>>>>>>>>" + size);
buffer = new byte[size];
fIn.read(buffer);
fIn.close();
} catch (IOException e) {
// TODO Auto-generated catch block
}
String path = "/sdcard/media/audio/ringtones/";
String filename = "examplefile" + ".ogg";
boolean exists = (new File(path)).exists();
if (!exists) {
System.out
.println("<<<<<<<FALSE SO INSIDE THE CONDITION>>>>>>>>>>>>>>>>>>>>");
new File(path).mkdirs();
}
FileOutputStream save;
try {
save = new FileOutputStream(path + filename);
System.out
.println("<<<<<<<SAVE>>>>>>>>>>>>>>>>>>>>" + save);
save.write(buffer);
save.flush();
save.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
Uri.parse("file://" + path + filename)));
File k = new File(path, filename);
System.out.println("<<<<<<<SAVE>>>>>>>>>>>>>>>>>>>>" + k);
}
});
}
}
在xml文件中有 sing按鈕,當我點擊按鈕時,文件才會移動。代碼執行沒有問題,但是文件就是移動不了。
uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"