clcurie_11 2013-03-15 01:33 采纳率: 6.7%
浏览 3216
已采纳

不能把一个文件从原始文件夹移动到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条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?