I write a program,which contains broadcast receiver to catch ACTION_MEDIA_MOUNTED,but it works not on all devices. In example: my Meizu m2 note has otg support , and device can open a flash storage,but in my application it not works(no open). In my code,I need to get directory(mass storage directory) path to write or read file.
my broadcast: public class UsbReceiver extends BroadcastReceiver {
private static final String ACTION_USB_PERMISSION = "com.poudanen.usbchecker.USB_PERMISSION";
@Override
public void onReceive(final Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_MEDIA_MOUNTED)) {
Toast.makeText(context, "Media device connected", Toast.LENGTH_SHORT).show();
Uri uriFile = intent.getData();
File dir = new File(uriFile.getPath());
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs.edit().putString("path", uriFile.getPath()).apply();
Toast.makeText(context, "Media device connected", Toast.LENGTH_SHORT).show();
if (dir.canRead()) {
//other code
}
}}