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;
}
}