title: USBdevice recognise as storage device and find path
body: <p>How can I detect a mounted device such as a Pen-Drive, that can be used for storage? How can I find the path for the mounted storage device so I may read files from it?</p>
  
  <p>I've used following broadcast receiver taking the permission to access mounted device:</p>
  
  <pre><code>private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
  
      public void onReceive(Context context, Intent intent) {
          String action = intent.getAction();
          if (ACTION_USB_PERMISSION.equals(action)) {
              synchronized (this) {
                  UsbDevice device = (UsbDevice) intent
                          .getParcelableExtra(UsbManager.EXTRA_DEVICE);
                  if (intent.getBooleanExtra(
                          UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                      if (device != null) {
                          // call method to set up device communication
  
                          Log.d(TAG, "onReceive: "+intent.getExtras().toString());
                          Log.d(TAG, "onReceive: "+intent.getData());
                          LinearLayout layoutUsbList = (LinearLayout)findViewById(R.id.layout_usb_list);
                          Button btn = new Button(MainActivity.this);
                          btn.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                          layoutUsbList.addView(btn);
                          btn.setText(device.getDeviceId()+"\t"+device.getDeviceName());
                          Log.d(TAG, "onReceive: "+intent.getExtras().toString());
                          final String path = intent.getData().getPath();
                          Log.e(TAG, "onReceive: path of device received from intent: "+ path );
                          btn.setOnClickListener(new OnClickListener() {
                              @Override
                              public void onClick(View v) {
                                  File file = new File(path);
                                  Toast.makeText(MainActivity.this, "file exists --&gt; "+file.exists()+"", Toast.LENGTH_SHORT).show();
                                  Toast.makeText(MainActivity.this, "file is directory --&gt; "+file.isDirectory()+"", Toast.LENGTH_SHORT).show();
                                  Log.d(TAG, "onClick: file is directory --&gt; "+file.isDirectory()+"");
                                  try{
                                      Toast.makeText(MainActivity.this, file.listFiles().length+"", Toast.LENGTH_LONG).show();
                                  }catch(Exception e){
                                      Toast.makeText(MainActivity.this, "error while showing total items", Toast.LENGTH_SHORT).show();
                                  }
  
                              }
                          });
  
                      }
                  } else {
                      Log.d("ERROR", "permission denied for device " + device);
                  }
              }
          }
      }
  };
  </code></pre>
  
  <p>Manifest file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  </code></pre>
  
  <p></p>
  
  <pre><code>&lt;uses-feature android:name="android.hardware.usb.host"/&gt;
  
  &lt;application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme"&gt;
      &lt;activity
          android:name=".MainActivity"
          android:configChanges="keyboard|orientation"
          android:label="@string/app_name"&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.MAIN"/&gt;
              &lt;category android:name="android.intent.category.LAUNCHER"/&gt;
          &lt;/intent-filter&gt;
  
  
          &lt;intent-filter&gt;
              &lt;action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"/&gt;
          &lt;/intent-filter&gt;
  
          &lt;meta-data
              android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
              android:resource="@xml/device_filter"/&gt;
  
          &lt;intent-filter&gt;
              &lt;action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED"/&gt;
          &lt;/intent-filter&gt;
  
  
          &lt;!----&gt;
          &lt;!----&gt;
          &lt;!----&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.MEDIA_MOUNTED"/&gt;
              &lt;data android:scheme="file"/&gt;
          &lt;/intent-filter&gt;
      &lt;/activity&gt;
  
  &lt;/application&gt;
  </code></pre>
  
  <p></p>