clcurie_11
2013-03-15 01:33
采纳率: 64.8%
浏览 3.2k

不能把一个文件从原始文件夹移动到SD卡

我使用下面的代码把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按钮,当我点击按钮时,文件才会移动。代码执行没有问题,但是文件就是移动不了。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • Sassoon 2013-03-15 09:28
    已采纳
    uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" 
    uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
    
    评论
    解决 无用
    打赏 举报
查看更多回答(3条)

相关推荐 更多相似问题