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();        }

更多相关文章

  1. findlibrary returned null产生的联想,Android(安卓)ndk开发打包
  2. Android工程的编译过程
  3. Android(安卓)应用开发笔记 - Android的系统构架
  4. Android(安卓)So简单加固
  5. Android覆盖升级以及apk签名
  6. Android中图片实现按钮点击效果
  7. 在Android中调用WebService
  8. 说说android下TV版本UC浏览器模拟鼠标的实现
  9. NPM 和webpack 的基础使用

随机推荐

  1. 【雨松MOMO】Android(安卓)软件开发与游
  2. Android除了三大动画,还有哪些动画?
  3. android流媒体播放器
  4. Android(安卓)XML解析学习——Dom方式
  5. Android混合开发-(Android与Web的交互)
  6. Android(安卓)Alarm驱动源代码分析(Alarm.
  7. Android帮助文档翻译——开发指南(一)应用
  8. Android(安卓)匿名共享内存Java接口分析
  9. android开发2:eclipse 开发 Android(安卓)
  10. 无废话Android之android下junit测试框架