1

我正在尝试复制文件列表中的文件。当用户长按列表中的项目时,我想复制选定的文件。

    public class MainActivity extends ListActivity {

private File file;
private List<String> myList;

String rootsd = Environment.getExternalStorageDirectory().toString();
String old = "/Android/data/co.vine.android/cache/videos";
String dirNameSlash = "/videos/";
String path = Environment.getExternalStorageDirectory() + dirNameSlash;
String newDir = "/Saved";
String olddir2 = new String( rootsd + old );
String newdir2 = new String( rootsd + newDir);
File temp_dir = new File(newDir);


public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    ListView lv = getListView();
    lv.setOnItemLongClickListener(new OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,int row, long arg3) {

            String item = (String) getListAdapter().getItem(row);
           // CopyFile
            Toast.makeText(getApplicationContext(), "File has been copied", Toast.LENGTH_LONG).show();

            return true;
        }
    });

    if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
        Log.d("MyApp", "No SDCARD");
} else {
    File directory = new File(Environment.getExternalStorageDirectory()+File.separator+"Saved");
    directory.mkdirs();

}

    myList = new ArrayList<String>();   

    String root_sd = Environment.getExternalStorageDirectory().toString();
    file = new File( root_sd + "/videos" ) ;       
    File list[] = file.listFiles();

    for( int i=0; i< list.length; i++)
    {
            myList.add( list[i].getName() );
    }

    setListAdapter(new ArrayAdapter<String>(this,
            R.layout.row, myList )); 
}

protected void onListItemClick(ListView l, View v, int position, long id) 
{
    super.onListItemClick(l, v, position, id);
    String item = (String) getListAdapter().getItem(position);
    Intent intentToPlayVideo = new Intent(Intent.ACTION_VIEW);
    intentToPlayVideo.setDataAndType(Uri.parse(path + item), "video/*");
    startActivity(intentToPlayVideo);
}

@Override
public void onBackPressed() {
            MainActivity.this.finish();
    }

任何帮助都会很棒。用户选择的所有文件都将保存到同一目录中。我需要帮助确定用户选择的文件,然后复制它。谢谢!

4

2 回答 2

1

我推荐你使用 FileChanel,效率更高,速度更快。例子:

 try {
            String root_sd = Environment.getExternalStorageDirectory().toString();
            String theFile = root_sd + "/" + (String) getListAdapter().getItem(row);
            File file = new File(theFile);
            FileInputStream source= new FileInputStream(file);
            String targetDirectory = Environment.getExternalStorageDirectory().toString();
            FileOutputStream destination = new FileOutputStream(targetDirectory + "/videos/" + file.getName);
            FileChannel sourceFileChannel = source.getChannel();
            FileChannel destinationFileChannel = destination.getChannel();
            long size = sourceFileChannel.size();

            sourceFileChannel.transferTo(0, size, destinationFileChannel);


        } catch (Exception ex) {
            Logger.getLogger(Borrar.class.getName()).log(Level.SEVERE, null, ex);
        }
于 2013-09-15T21:46:09.477 回答
0
try
{
    File f1 = new File(srFile);
    File f2 = new File(dtFile);
    InputStream in = new FileInputStream(f1);
    OutputStream out = new FileOutputStream(f2);

    byte[] buf = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0)
    {
        out.write(buf, 0, len);
    }
    in.close();
    out.close();
    System.out.println("File copied.");
}
catch(FileNotFoundException ex)
{
    //print in logcat 
}
catch(IOException e)
{
    //print in logcat 
}
于 2013-09-15T21:08:28.557 回答