如何将文件从listview移动到目录?

时间:2013-02-18 05:28:34

标签: android android-listview

我创建了一个列表视图,显示了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());

             }
    }  
}

请再提出建议..

1 个答案:

答案 0 :(得分:0)

您无需在路径中添加/mnt,因此只需编辑以下行

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

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

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

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

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

if ( !toFile.exists() )
{
     toFile.createNewFile();
}