系统:安卓12
屏蔽sd卡/内存存储显示
packages/services/Mtp/src/com/android/mtp/MtpService.java
修改如下,屏蔽了内部存储,如果需要屏蔽sd卡也可以根据这里做修改:
java
private void addStorage(StorageVolume volume) {
Log.v(TAG, "Adding MTP storage:" + volume.getPath());
//LIN
if(volume.getPath().contains("/storage/emulated/")) {
Log.d(TAG, "不添加 内部路径 "+volume.getPath());
return;
}
synchronized (MtpService.class) {
if (sServerHolder != null) {
sServerHolder.database.addStorage(volume);
}
}
if (mPerfBoost == null) {
mPerfBoost = new BoostFramework(true);
}
if (mPerfBoost != null && !mIsPerfLockAcquired) {
//Use big enough number here to hold the perflock for entire MTP session
mPerfBoost.perfHint(BoostFramework.VENDOR_HINT_MTP_BOOST, null, Integer.MAX_VALUE, -1);
mIsPerfLockAcquired = true;
}
}
只展示特定文件夹和文件类型
参考链接:Android 11 mtp文件夹自定义显示,且不可写,不可删除_java android 如何让文件夹不被删除-CSDN博客
frameworks/base/media/java/android/mtp/MtpPropertyGroup.java
修改只展示某个app里面的视频、图片内容,并且过滤文件类型
java
class MtpPropertyGroup {
private static final String TAG = MtpPropertyGroup.class.getSimpleName();
//add by lj start LIN
private static String DIRECTORY_RECORD = "Android";
private static String DIRECTORY_RECORD1 = "data";
private static String DIRECTORY_RECORD2 = "com.example.xxx";
private static String DIRECTORY_RECORD3 = "files";
private static String DIRECTORY_RECORD4 = "video";
private static String DIRECTORY_RECORD5 = "picture";
private static String DIRECTORY_RECORD6 = "audio";
//add by lj end
private class Property {
int code;
int type;
int column;
Property(int code, int type, int column) {
this.code = code;
this.type = type;
this.column = column;
}
}
java
switch (property.code) {
case MtpConstants.PROPERTY_PROTECTION_STATUS:
// protection status is always 0
list.append(id, property.code, property.type, 0);
break;
case MtpConstants.PROPERTY_NAME:
case MtpConstants.PROPERTY_OBJECT_FILE_NAME:
//add by lj start LIN
if (object.getParent().isRoot() && !DIRECTORY_RECORD.equals(object.getName())) {
responseId = MtpConstants.RESPONSE_INVALID_OBJECT_HANDLE;
break;
} else if (!object.getParent().isRoot() &&
!(
DIRECTORY_RECORD1.equals(object.getName())
|| DIRECTORY_RECORD2.equals(object.getName())
|| DIRECTORY_RECORD3.equals(object.getName())
|| DIRECTORY_RECORD4.equals(object.getName())
|| DIRECTORY_RECORD5.equals(object.getName())
|| DIRECTORY_RECORD6.equals(object.getName())
|| object.getName().contains("mp4")
|| object.getName().contains("jpg")
|| object.getName().contains("mp3")
|| object.getName().contains("wav")
|| object.getName().contains("aac")
)){
responseId = MtpConstants.RESPONSE_INVALID_OBJECT_HANDLE;
break;
}
//add by lj end
case MtpConstants.PROPERTY_DISPLAY_NAME:
list.append(id, property.code, object.getName());
break;