1

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
        }
    }}
Yurii
  • 552
  • 3
  • 14
  • try this http://stackoverflow.com/questions/7142987/media-mounted-broadcast-not-being-received – Amir May 16 '16 at 13:05
  • Did you add permission? – Amir May 16 '16 at 13:05
  • @Amir,yep,I did) in my case i want to use allert dialog in my broadcast,and my broadcast called in activity. Your first answer not helped me(( – Yurii May 16 '16 at 13:11

0 Answers0