usb连接的PTP模式,同时显示内置、外置SD卡内容
16lz
2021-01-23
原生态的android中,PTP只支持本地存储设备,外部sd设备没有添加。但可以通过修改代码添加,前提是你必须要有android的源码,我的是在MTK平台下面开发的。
修改文件:
1:...\os\Enviroment.java
添加下面两行代码
public static String DIRECTORY_SD2_PICTURES = "Pictures2"; public static String DIRECTORY_SD2_DCIM = "DCIM2";
2: packages\privoiders\MediaProvider\src\com\android\providers\media\MtpService.java //add for two PTP disk. start. private static final String[] PTP_DIRECTORIES_SD2 = new String[] { Environment.DIRECTORY_SD2_DCIM, Environment.DIRECTORY_SD2_PICTURES, }; //add for two PTP disk. end.
3. private void addStorageDevicesLocked() { /*if (mPtpMode) { // In PTP mode we support only primary storage final StorageVolume primary = StorageManager.getPrimaryVolume(mVolumes); final String path = primary.getPath(); if (path != null) { String state = mStorageManager.getVolumeState(path); if (Environment.MEDIA_MOUNTED.equals(state)) { addStorageLocked(mVolumeMap.get(path)); } } } else cancelled. for PTP two disk. */{ for (StorageVolume volume : mVolumeMap.values()) { addStorageLocked(volume); } } } 4. @Override public int onStartCommand(Intent intent, int flags, int startId) { synchronized (mBinder) { updateDisabledStateLocked(); isUsbConfigured = (intent == null ? false :intent.getBooleanExtra(UsbManager.USB_CONFIGURED, false)); mPtpMode = (intent == null ? false : intent.getBooleanExtra(UsbManager.USB_FUNCTION_PTP, false)); String[] subdirs = null; if (mPtpMode) { int count = PTP_DIRECTORIES.length; int count2 = PTP_DIRECTORIES_SD2.length; //add. for PTP two disks. subdirs = new String[count+count2]; //add. for PTP two disk. for (int i = 0; i < count; i++) { File file = Environment.getExternalStoragePublicDirectory(PTP_DIRECTORIES[i]); // make sure this directory exists file.mkdirs(); subdirs[i] = file.getPath(); } for (int i = count; i < count+count2; i++) { //modify. for PTP two disks. File file = new File(System.getenv("SECONDARY_STORAGE"), PTP_DIRECTORIES_SD2[i-count]); //wqtao. modify. for PTP two disks. // make sure this directory exists file.mkdirs(); subdirs[i] = file.getPath(); } } 5. private void volumeMountedLocked(String path) { // Add for update Storage StorageVolume[] volumes = mStorageManager.getVolumeList(); mVolumes = volumes; // Add for update Storage for (int i = 0; i < mVolumes.length; i++) { StorageVolume volume = mVolumes[i]; if (volume.getPath().equals(path)) { mVolumeMap.put(path, volume); if (!mMtpDisabled) { // In PTP mode we support only primary storage /*if (volume.isPrimary() || !mPtpMode) cancelled. for PTP two disk. */{ addStorageLocked(volume); } } break; } } }
先./mk [Your ProjectName] update-api,再remake. 更多相关文章
- 【代码】Android: 怎样设置app不被系统k掉
- android 源代码定制--- 状态栏
- Android 检测键盘显示或隐藏键盘的实现代码
- android launcher源码分析
- Android异步加载源码示例