0

我创建了一个列表视图,显示 sd 卡中的所有音频文件。现在我想将该列表中的任何文件移动到名为“Ankit_s”的文件夹中,但是在运行以下代码时,我遇到了这样的错误,

02-18 10:20:26.980: E/readfile(407): /sdcardasd.mp3 (No such file or directory)

我的代码在onListItemClick()

public class Pick extends ListActivity {


    //Setting SD card path. this is the place from where we retrieve the audio files
        private static final String MEDIA_PATH = new String(Environment.getExternalStorageDirectory().getPath());
        // To hold all songs   
        private List<String> songs = new ArrayList<String>();
        //To find out the Index of File, Currently selected one.
        private int currentPosition = 0;


    @Override

    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user);

        //Below method bind all songs to ListView
        BindAllSongs();


    }

    private void BindAllSongs() {
        // TODO Auto-generated method stub
        //To hold all the audio files from SDCARD
                File fileListBeforeFiltering = new File(MEDIA_PATH);
                //Filter all the MP# format songs to list out
                //Checking for the MP3 Extension files existence 
                    if (fileListBeforeFiltering.listFiles(new FilterFilesByMp3Extension()).length > 0) 
                    {
                        //Loop thru all the files filtered and fill them in SongsList view that we have 
                        //Defined above.
                        for (File file : fileListBeforeFiltering.listFiles(new FilterFilesByMp3Extension())) 
                        {
                            //Adding all filtered songs to the list
                                songs.add(file.getName());
                        }

                        //Loading them to Listview using ArrayAdapter.

                        ArrayAdapter<String> allsongs = new ArrayAdapter<String>(this,R.layout.song_items, songs);
                        setListAdapter(allsongs);
                }

    }

    // Responding to OnClick Event to Listview
    @Override   
    protected void onListItemClick(ListView l, View v, int position, long id) {
        //Hold the currently clicked Item [Sending Path and Current song from list view
        currentPosition = position;




           try {
               File direct = new File(Environment.getExternalStorageDirectory() + File.separator+"Ankit_s");

               if(!direct.exists())
                {
                    if(direct.mkdir()); //directory is created;

                }


               File sourcepath = new File(MEDIA_PATH + songs.get(position));


               File fromFile=new File(sourcepath.getName());

               File toFile=new File(Environment.getExternalStorageDirectory()+ File.separator+"Ankit",fromFile.getName());

             java.io.FileInputStream fosfrom = new FileInputStream(fromFile);

             java.io.FileOutputStream fosto = new FileOutputStream(toFile);

             byte bt[] = new byte[1024];

             int c;

             while ((c = fosfrom.read(bt)) > 0) {

             fosto.write(bt, 0, c); 

             }

             Toast.makeText(Pick.this," moved successfully", Toast.LENGTH_LONG).show();

             fromFile.deleteOnExit();

            fosfrom.close();

            fosto.close();

            } catch (Exception ex) {

            Log.e("readfile", ex.getMessage());

             }
    }  
}

请再给点建议。。

4

1 回答 1

0

您不需要添加/mnt路径,因此只需编辑以下行

File toFile=new File("/mnt/sdcard/Ankit_s/",fromFile.getName());

File toFile=new File("/sdcard/Ankit_s/",fromFile.getName());

现在它会正常工作。您还需要WRITE_EXTERNAL_STORAGE在您的 AndroidManifest.xml 文件中授予权限。

编辑,你需要检查文件是否存在,如果不存在,你需要按照以下语法创建新的,

File toFile=new File("/sdcard/Ankit_s/",fromFile.getName());

if ( !toFile.exists() )
{
     toFile.createNewFile();
} 
于 2013-02-18T05:55:17.043 回答