Advertisement
icorrelate

Java Ext to Cp

Jul 15th, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.85 KB | None | 0 0
  1. package xyz.itcstutorial.externaltoandroid;
  2.  
  3. import android.content.BroadcastReceiver;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.IntentFilter;
  7. import android.hardware.usb.UsbManager;
  8. import android.support.v7.app.AppCompatActivity;
  9. import android.os.Bundle;
  10. import android.widget.TextView;
  11. import android.widget.Toast;
  12.  
  13. import java.io.File;
  14. import java.io.FileInputStream;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.io.OutputStream;
  19. import java.util.ArrayList;
  20. import java.util.Calendar;
  21. import java.util.List;
  22.  
  23. public class MainActivity extends AppCompatActivity {
  24.  
  25.     private TextView txtInfo;
  26.  
  27.     @Override
  28.     protected void onCreate(Bundle savedInstanceState) {
  29.         super.onCreate(savedInstanceState);
  30.         setContentView(R.layout.activity_main);
  31.  
  32.         txtInfo = (TextView)findViewById(R.id.txtinfo);
  33.  
  34.         BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
  35.             public void onReceive(Context context, Intent intent) {
  36.                 String action = intent.getAction();
  37.  
  38.                 try{
  39.  
  40.                     if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
  41.                         Toast.makeText(MainActivity.this, "USB Connected \n" , Toast.LENGTH_SHORT).show();
  42.  
  43.  
  44.                         //OTG Path
  45.                         String path = "/storage/usbotg";
  46.  
  47.                         //Destination of Files. Create a directory if destination not exists.
  48.                         String dest = "/storage/sdcard0/" + Calendar.getInstance().getTimeInMillis();
  49.                         File destination = new File(dest);
  50.                         if(!destination.exists()){
  51.                             destination.mkdir();
  52.                         }
  53.  
  54.                         //Directory's Path.
  55.                         File directory = new File(path);
  56.                         txtInfo.append("\nFiles Path: " + path);
  57.  
  58.                         //Wait while flash drive is not ready yet.
  59.                         File[] files = directory.listFiles();
  60.                         while(files == null){
  61.                             files = directory.listFiles();
  62.                         }
  63.                         txtInfo.append("\nFiles Size: " +  files.length);
  64.  
  65.  
  66.                         //Loop through the paths.
  67.                         for (int i = 0; i < files.length; i++)
  68.                         {
  69.                             String filePath = files[i].getAbsolutePath();
  70.                             String fileName = files[i].getName().toString();
  71.                             String extension = getExtension(fileName);
  72.  
  73.                             File toCopy = new File(filePath);
  74.  
  75.                             //If path is a file, Check it's extension and copy it.
  76.                             if(toCopy.isFile()){
  77.                                 List<String> validExt = new ArrayList<>();
  78.                                 validExt.add("txt");
  79.                                 if(validExt.contains(extension)){
  80.                                     copy(toCopy, new File(dest + "/" + fileName));
  81.                                     txtInfo.append("\nFileName:" + filePath + " is copied!");
  82.                                 }
  83.                             }
  84.                         }
  85.  
  86.                     } else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
  87.                         Toast.makeText(MainActivity.this, "USB Disconnected", Toast.LENGTH_SHORT).show();
  88.                     }
  89.                 }catch (Exception ex){
  90.                     Toast.makeText(MainActivity.this, ex.getMessage().toString(), Toast.LENGTH_SHORT).show();
  91.                 }
  92.  
  93.             }
  94.  
  95.  
  96.         };
  97.  
  98.         //Register the broadcast receiver in the current context
  99.         IntentFilter filter = new IntentFilter();
  100.         filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
  101.         filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
  102.         this.registerReceiver(mUsbReceiver, filter);
  103.  
  104.  
  105.  
  106.     }
  107.  
  108.  
  109.     public String getExtension(String filename){
  110.         String extension = "";
  111.  
  112.         int i = filename.lastIndexOf('.');
  113.         if (i > 0) {
  114.             extension = filename.substring(i+1);
  115.         }
  116.         return extension;
  117.     }
  118.  
  119.  
  120.     //code to copy a file.
  121.     public void copy(File src, File dst) throws IOException {
  122.         InputStream in = new FileInputStream(src);
  123.         try {
  124.             OutputStream out = new FileOutputStream(dst);
  125.             try {
  126.                 byte[] buf = new byte[1024];
  127.                 int len;
  128.                 while ((len = in.read(buf)) > 0) {
  129.                     out.write(buf, 0, len);
  130.                 }
  131.             } finally {
  132.                 out.close();
  133.             }
  134.         } finally {
  135.             in.close();
  136.         }
  137.     }
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement