1

当我通过 OTG usb 将闪存驱动器插入我的平板电脑或手机时,我正在制作一个 android 应用程序,它会将文件从它传输到指定位置。现在,如果我指定一个文件名,它将传输,但我怎样才能让它传输所有文件而不必指定一个名称?我正在使用带有字节数组的文件输入/文件输出。

@SuppressLint("SdCardPath")

公共类文件传输扩展 Activity {

Button btnsend;
String extstorage = Environment.getExternalStorageDirectory().toString();



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activityfiletransfer);
    btnsend = (Button) findViewById(R.id.button1);

    final Runtime runtime = Runtime.getRuntime(); 
    try
    {
        runtime.exec("su -c");
    }
    catch(Exception e)
    {

    }



    btnsend.setOnClickListener(new View.OnClickListener()
    {
        Context context = getApplicationContext();
        CharSequence scs = "Folder Doesn't Exist, Creating now, Checking for Flash Drive....";
        CharSequence mvng = "Folder already exist, checking now for flash drive....";
        CharSequence chk1 = "Flash Drive detected, transferring files now.";
        CharSequence chk2 = "Flash Drive Is Not Detected";
        int duration = Toast.LENGTH_SHORT;
        Toast bldng = Toast.makeText(context, scs, duration);
        Toast mvfldr = Toast.makeText(context, mvng, duration);
        Toast chkffd = Toast.makeText(context, chk1, duration);
        Toast chkffd2 = Toast.makeText(context, chk2, duration);

        @Override
        public void onClick(View arg0)
        {
            File src = new File(extstorage + "/FILEBACKUP");
            File usbdrive = new File(extstorage + "/usbStorage/sda1");
            if(!src.exists())
            {
                src.mkdirs();
                bldng.show();
                if(!usbdrive.exists())
                {
                    chkffd2.show();
                }
                else
                {
                    chkffd.show();
                    copyfiles();
                }

            }

            else
            {

                mvfldr.show();
                if(!usbdrive.exists())
                {
                    chkffd2.show();
                }
                else
                {
                    chkffd.show();
                    copyfiles();
                }

            }
    }
        private void copyfiles()
        {
            String pathofd = "/sdcard/usbStorage/sda1";
            String internalpath = "/sdcard/FILEBACKUP";

            File dir = new File(pathofd);
            File[] files = dir.listFiles();
            for (File afile : files)
            {
                if(afile.isFile())
                {
                    copyFile(afile.getAbsolutePath(), internalpath, afile.getName());
                }
            }


            try
            {


            }

                /*OutputStream myoutput = new FileOutputStream("/sdcard/FILEBACKUP");
                InputStream myinput = new FileInputStream("/sdcard/usbStorage/");
                byte[] buffer = new byte[1024];
                int length;
                while((length=myinput.read(buffer))>0)
                {
                    myoutput.write(buffer);
                }
                myoutput.flush();

                myoutput.close();

                myinput.close();

            }*/
            catch(Exception e)
            {

            }
        }
        private void copyFile(String absolutePath, String internalpath,
                String name) {
            // TODO Auto-generated method stub

            try
            {
                OutputStream myoutput = new FileOutputStream(extstorage + "/FILEBACKUP/");
                InputStream myinput = new FileInputStream(extstorage + "/usbStorage/sda1");
                byte[] buffer = new byte[1024];
                int length;
                while((length=myinput.read(buffer))>0)
                {
                    myoutput.write(buffer, 0, length);
                }
                myoutput.flush();

                myoutput.close();

                myinput.close();

            }
            catch(Exception e)
            {

            }

        }




        });
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activityfiletransfer,
                menu);
        return true;
    }
}
4

1 回答 1

1

只需列出目录中的文件:

String pathOfFlashDrive = <path of flash drive goes here>;
String internalPath = <internal destination path goes here>;

File dir = new File( pathOfFlashDrive );
File[] files = dir.listFiles();
for( File aFile : files ) {
    if( aFile.isFile()  ) {
        copyFile( aFile.getAbsolutePath(), internalPath + aFile.getName() );
    }
}
于 2012-12-07T07:33:17.893 回答