Java 如何在android中获得路径挂载usb设备?
我编写了一个程序,其中包含一个广播接收器,用于捕捉安装在上的动作媒体,但它并不适用于所有设备。例如:我的Meizu m2 note支持otg,并且设备可以打开闪存,但在我的应用程序中它不工作(没有打开)。在我的代码中,我需要获取目录(大容量存储目录)路径来写入或读取文件 我的广播: 公共类UsbReceiver扩展广播接收器{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
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对话框,我的广播在活动中调用。你的第一个回答对我没有帮助((