如何取得USB插入装置的路径

bangayegg 2016-01-27 02:47:47
小弟最近在做一个功能,就是当Android装置插上USB随身碟后,

在我自己开发的程式里面开启USB随身碟的资料,目前功能部份我是有做出来,

但在路径上我是写死的,我事先先用ES档案管理器去看USB插上后的路径再写入程式中,

但是如果插上不同随身碟的话,有的路径会不相同导致读取不到档案,

想请问一下大大有人知道如何取得正确路径吗?
...全文
488 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
peter_nj_RD 2016-01-28
  • 打赏
  • 举报
回复
可以参看 Google原生的StorageNotification.java这个类,里面有完整的实现,Android L和M版本关于存储这部分改动的挺大的
qq_26517699 2016-01-28
  • 打赏
  • 举报
回复
顶一下,我最近也在写一个挂载U盘的读取,也不知道路径怎么获取
huangxiaohu_coder 2016-01-28
  • 打赏
  • 举报
回复
[quote=引用 8 楼 bangayegg 的回复:] 用这个试试呢 StorageManager sm = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE); String[] volumePaths = (String[]) sm.getClass().getMethod("getVolumePaths", null).invoke(sm, null);
bangayegg 2016-01-28
  • 打赏
  • 举报
回复
引用 1 楼 huangxiaohu_coder 的回复:
final IMountService mountService = IMountService.Stub .asInterface(ServiceManager.getService("mount")); final StorageVolume[] volumes = mountService.getVolumeList(); for (StorageVolume volume : volumes) { 遍历时有一个路径就是你的U disk被mount的位置(volume .getPath()) }
请问大大可否给个更完整一点的Code当参考呢?小弟基础不太好XD
qq_26517699 2016-01-28
  • 打赏
  • 举报
回复
刚才我找了一个例子 ,我才自学没看懂 你可以试试
package com.control.toolmonitoring.manage;

import com.control.toolmonitoring.LogPrint;
import com.control.toolmonitoring.MainActivity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Message;
import android.util.Log;

/**监听U盘是否插入,消息传到MainActivity*/

public class UsbStatesReceiver extends BroadcastReceiver {
MainActivity execactivity;

public static final int USB_STATE_MSG = 0x00020;
public static final int USB_STATE_ON = 0x00021;
public static final int USB_STATE_OFF = 0x00022;
public IntentFilter filter = new IntentFilter();

public UsbStatesReceiver(Context context) {
execactivity = (MainActivity) context;
filter.addAction(Intent.ACTION_MEDIA_CHECKING);
filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
filter.addAction(Intent.ACTION_MEDIA_EJECT);
filter.addAction(Intent.ACTION_MEDIA_REMOVED);
filter.addDataScheme("file");
}

// 动态注册广播
public Intent registerReceiver() {
return execactivity.registerReceiver(this, filter);
}

// 注销广播
public void unregisterReceiver() {
execactivity.unregisterReceiver(this);
}

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (execactivity.mhandler == null) {
return;
}

Message msg = new Message();
msg.what = USB_STATE_MSG;

if (intent.getAction().equals(Intent.ACTION_MEDIA_MOUNTED)
|| intent.getAction().equals(Intent.ACTION_MEDIA_CHECKING)) {
msg.arg1 = USB_STATE_ON;
} else {
msg.arg1 = USB_STATE_OFF;
}
execactivity.mhandler.sendMessage(msg);

//就是下面一句代码找到的路径,上面是我找的监听挂载U盘插入的例子
final String path = intent.getData().getPath();
intent = new Intent(Intent.ACTION_MEDIA_SHARED, Uri.parse("file://" + path));
LogPrint.d("0x05", intent);
};

}

我的打印结果是
2016-01-28 19:04:39 d 0x05 Intent { act=android.intent.action.MEDIA_SHARED dat=file:///storage/sda4 }

2016-01-28 19:04:43 d 0x05 Intent { act=android.intent.action.MEDIA_SHARED dat=file:///storage/sda4 }

路径是storage/sda4,正确的,楼主可以试试

http://blog.csdn.net/veryitman/article/details/7603428这是我刚才看的文章 挺不错的
还请大神请教
bangayegg 2016-01-28
  • 打赏
  • 举报
回复
MountService 似乎找不到这个类呢?
bangayegg 2016-01-28
  • 打赏
  • 举报
回复
就一开始是希望希望大大可以写一个简单的例子XD,不过小弟会再仔细去研究大大提供的这部份及相关资料,感谢您^^
huangxiaohu_coder 2016-01-28
  • 打赏
  • 举报
回复
引用 3 楼 bangayegg 的回复:
[quote=引用 1 楼 huangxiaohu_coder 的回复:] final IMountService mountService = IMountService.Stub .asInterface(ServiceManager.getService("mount")); final StorageVolume[] volumes = mountService.getVolumeList(); for (StorageVolume volume : volumes) { 遍历时有一个路径就是你的U disk被mount的位置(volume .getPath()) }
请问大大可否给个更完整一点的Code当参考呢?小弟基础不太好XD[/quote] 这段code就是实际的code了啊,主要就是通过mountservice来获取,你是哪里不太清楚呢
我的学生时代 2016-01-27
  • 打赏
  • 举报
回复
为什么至少10个字?可以用反射
huangxiaohu_coder 2016-01-27
  • 打赏
  • 举报
回复
final IMountService mountService = IMountService.Stub .asInterface(ServiceManager.getService("mount")); final StorageVolume[] volumes = mountService.getVolumeList(); for (StorageVolume volume : volumes) { 遍历时有一个路径就是你的U disk被mount的位置(volume .getPath()) }

80,349

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧