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条)

报告相同问题?

悬赏问题

  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划