将文件从USB传输到内部存储器?

时间:2012-12-07 07:08:03

标签: android fileinputstream fileoutputstream usb-drive

当我通过OTG usb将闪存驱动器插入我的平板电脑或手机时,我正在创建一个Android应用程序,它会将文件传输到指定位置。现在,如果我指定一个文件名,它将被转移,但是如何让它传输所有文件而不必指定名称?我正在使用fileinput / fileoutput和字节数组。

@SuppressLint("SdCardPath")

public class filetransfer extends 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;
    }
}

1 个答案:

答案 0 :(得分: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() );
    }
}