Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在android中获得路径挂载usb设备?_Java_Android_Broadcast_Host - Fatal编程技术网

Java 如何在android中获得路径挂载usb设备?

Java 如何在android中获得路径挂载usb设备?,java,android,broadcast,host,Java,Android,Broadcast,Host,我编写了一个程序,其中包含一个广播接收器,用于捕捉安装在上的动作媒体,但它并不适用于所有设备。例如:我的Meizu m2 note支持otg,并且设备可以打开闪存,但在我的应用程序中它不工作(没有打开)。在我的代码中,我需要获取目录(大容量存储目录)路径来写入或读取文件 我的广播: 公共类UsbReceiver扩展广播接收器{ private static final String ACTION_USB_PERMISSION = "com.poudanen.usbchecker.USB_PERM

我编写了一个程序,其中包含一个广播接收器,用于捕捉安装在上的动作媒体,但它并不适用于所有设备。例如:我的Meizu m2 note支持otg,并且设备可以打开闪存,但在我的应用程序中它不工作(没有打开)。在我的代码中,我需要获取目录(大容量存储目录)路径来写入或读取文件

我的广播: 公共类UsbReceiver扩展广播接收器{

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
        }
    }}

试试这个你添加权限了吗?@Amir,是的,我有)在我的情况下,我想在广播中使用allert对话框,我的广播在活动中调用。你的第一个回答对我没有帮助((试试这个你添加权限了吗?@Amir,是的,我有)在我的情况下,我想在广播中使用allert对话框,我的广播在活动中调用。你的第一个回答对我没有帮助((