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());
}
}
}
もう提案してください..