Android8.1以及5.1版本识别sdcard和U盘并创建文件解决办法

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百万案例之路,android,安卓,android,studio,Android读写文件,Android挂载U盘)