Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package xyz.itcstutorial.externaltoandroid;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.hardware.usb.UsbManager;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.widget.TextView;
- import android.widget.Toast;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.ArrayList;
- import java.util.Calendar;
- import java.util.List;
- public class MainActivity extends AppCompatActivity {
- private TextView txtInfo;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- txtInfo = (TextView)findViewById(R.id.txtinfo);
- BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
- public void onReceive(Context context, Intent intent) {
- String action = intent.getAction();
- try{
- if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
- Toast.makeText(MainActivity.this, "USB Connected \n" , Toast.LENGTH_SHORT).show();
- //OTG Path
- String path = "/storage/usbotg";
- //Destination of Files. Create a directory if destination not exists.
- String dest = "/storage/sdcard0/" + Calendar.getInstance().getTimeInMillis();
- File destination = new File(dest);
- if(!destination.exists()){
- destination.mkdir();
- }
- //Directory's Path.
- File directory = new File(path);
- txtInfo.append("\nFiles Path: " + path);
- //Wait while flash drive is not ready yet.
- File[] files = directory.listFiles();
- while(files == null){
- files = directory.listFiles();
- }
- txtInfo.append("\nFiles Size: " + files.length);
- //Loop through the paths.
- for (int i = 0; i < files.length; i++)
- {
- String filePath = files[i].getAbsolutePath();
- String fileName = files[i].getName().toString();
- String extension = getExtension(fileName);
- File toCopy = new File(filePath);
- //If path is a file, Check it's extension and copy it.
- if(toCopy.isFile()){
- List<String> validExt = new ArrayList<>();
- validExt.add("txt");
- if(validExt.contains(extension)){
- copy(toCopy, new File(dest + "/" + fileName));
- txtInfo.append("\nFileName:" + filePath + " is copied!");
- }
- }
- }
- } else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
- Toast.makeText(MainActivity.this, "USB Disconnected", Toast.LENGTH_SHORT).show();
- }
- }catch (Exception ex){
- Toast.makeText(MainActivity.this, ex.getMessage().toString(), Toast.LENGTH_SHORT).show();
- }
- }
- };
- //Register the broadcast receiver in the current context
- IntentFilter filter = new IntentFilter();
- filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
- filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
- this.registerReceiver(mUsbReceiver, filter);
- }
- public String getExtension(String filename){
- String extension = "";
- int i = filename.lastIndexOf('.');
- if (i > 0) {
- extension = filename.substring(i+1);
- }
- return extension;
- }
- //code to copy a file.
- public void copy(File src, File dst) throws IOException {
- InputStream in = new FileInputStream(src);
- try {
- OutputStream out = new FileOutputStream(dst);
- try {
- byte[] buf = new byte[1024];
- int len;
- while ((len = in.read(buf)) > 0) {
- out.write(buf, 0, len);
- }
- } finally {
- out.close();
- }
- } finally {
- in.close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement