Android8.1以及5.1版本识别sdcard和U盘并创建文件解决办法
16lz
2021-01-23
Android8.1以及5.1版本识别sdcard和U盘并创建文件解决办法
- 概述
- 完整代码:
概述
最近正好在写安卓下的测试工具,看到安卓里面各种版本的api各种不同,我还是真心希望Qt在跨平台方面加把劲。好了,对于Android8.1的系统,如果我们需要挂载U盘或者外部的sdcard,就需要使用到反射来处理了,另外为了能够自动识别这种挂载的行为,我们还需要实现一个广播接收器BroadcastReceiver。
完整代码:
Android5.1版本:SdcardUsbTest5.1.zip:
Android8.1版本:SdcardUsbTest8.1.zip:
Class<?> MyVolumeInfo = Class.forName("android.os.storage.VolumeInfo"); Class<?> MyStorageManager = Class.forName("android.os.storage.StorageManager"); Class<?> MyDiskInfo = Class.forName("android.os.storage.DiskInfo"); Method myGetVolumes = MyStorageManager.getMethod("getVolumes"); Method myGetDescriptionComparator = MyVolumeInfo.getMethod("getDescriptionComparator"); Method myGetType = MyVolumeInfo.getMethod("getType"); Method myGetDisk = MyVolumeInfo.getMethod("getDisk"); Method myIsSd = MyDiskInfo.getMethod("isSd"); Method myGetState = MyVolumeInfo.getMethod("getState"); Method myGetPath = MyVolumeInfo.getMethod("getPath"); myGetPath.setAccessible(true); Field TYPE_PUBLIC = MyVolumeInfo.getField("TYPE_PUBLIC"); TYPE_PUBLIC.setAccessible(true); Field STATE_MOUNTED = MyVolumeInfo.getField("STATE_MOUNTED"); STATE_MOUNTED.setAccessible(true);
private BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (Intent.ACTION_MEDIA_MOUNTED.equals(action)) { Uri uri = intent.getData(); assert uri != null; Log.i(TAG, "intent ACTION_MEDIA_MOUNTED uri=" + uri.getPath()); try { testDevice(); } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | java.lang.InstantiationException | NoSuchFieldException | ClassNotFoundException e) { e.printStackTrace(); } } else if (Intent.ACTION_MEDIA_BAD_REMOVAL.equals(action) || Intent.ACTION_MEDIA_UNMOUNTED.equals(action) || Intent.ACTION_MEDIA_REMOVED.equals(action)) { Uri uri = intent.getData(); assert uri != null; Log.i(TAG, "intent ACTION_MEDIA_UNMOUNTED uri=" + uri.getPath()); try { testDevice(); } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | java.lang.InstantiationException | NoSuchFieldException | ClassNotFoundException e) { e.printStackTrace(); } } } };
当你实现了自动识别挂载之后,读写文件又是一个比较麻烦的事情了,安卓在各个版本中,文件的读写基本都是不一样的,唉,改动这么大,你妈妈知道吗?所以,我们又不能使用以前的外部读写文件的方法了,得另寻蹊径。
testFilename = "test.txt"; String testFilePath = null; File directory = context.getExternalFilesDir(null); LOG("directory === " + directory.getAbsolutePath()); File[] externalStorageVolumes = ContextCompat.getExternalFilesDirs(context,null); for( File fileaa : externalStorageVolumes ){ LOG("files=====" + fileaa.getAbsolutePath() ); if( fileaa.getAbsolutePath().contains(storagePath)){ testFilePath = fileaa.getAbsolutePath() + testFilename; } } LOG(testFilePath + " createNewFile "); File testFile = new File(testFilePath); if (testFile.exists()) { if (!testFile.delete()) { LOG(testFile + "delete failed"); } } if (testFile.createNewFile()) { FileOutputStream fout = new FileOutputStream(testFile); OutputStreamWriter fOutWriter = new OutputStreamWriter(fout); String testContent = "nice to meet you"; fOutWriter.write(testContent); fOutWriter.close(); LOG(testFile + " create file success"); flag = true; } else { LOG(testFile + " create file failed"); } } catch (IOException e) { e.printStackTrace(); }
更多相关文章
- Android转场动画和共享元素动画兼容5.0以下版本的实现
- 使用ant让Android自动打包的build.xml,自动生成签名的apk文件(支
- 说说android下TV版本UC浏览器模拟鼠标的实现
- Android 事件全局监听(二)需要root权限 ,使用getevent监听Android输
- android 调用系统文件管理器
- Android运行时权限,6.0—9.0多版本,多终端(手机,TV盒子)130行代码一劳
- Android Studio下载、安装和配置+SDK+tools下载(无敌超级详细版
- Android 为【apk】文件签名,增加修改系统时间等权限
- Android程序版本控制工具类