【发布时间】:2014-02-19 13:10:34
【问题描述】:
我得到这个代码来将文件从 App 目录复制到 SD 卡上的文件夹,并使用该文件设置为铃声。 但是有一点我不明白。 我如何确定要使用哪个文件? 假设我在 raw 文件夹中有 1 个名为 fusrodah 的文件。 如何让我的应用选择该文件并将其复制到 sdcard 文件夹?
private int size;
private static final int BUFFER_LEN = 1024;
private void copyFile(AssetManager assetManager, String fileName, File out) throws FileNotFoundException, IOException {
size = 0;
FileOutputStream fos = new FileOutputStream(out);
InputStream is = assetManager.open(fileName);
int read = 0;
byte[] buffer = new byte[BUFFER_LEN];
while ((read = is.read(buffer, 0, BUFFER_LEN)) >= 0) {
fos.write(buffer, 0, read);
size += read;
}
fos.flush();
fos.close();
is.close();
}
public void onClick(View arg0) {
AssetManager assetManager = getAssets();
File file = new File(Environment.getExternalStorageDirectory(),
"/myRingtonFolder/Audio/");
if (!file.exists()) {
file.mkdirs();
}
String path = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/myRingtonFolder/Audio/";
File out = new File(path + "/", "fusrodah.mp3");
if(!out.exists()){
try {
copyFile(assetManager, "fusrodah.mp3", out);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
【问题讨论】: