Android 版本10以下存储和 10以上外部存储的区别,不得不说安卓碎片化真TMD的恶心
安卓10以下存储:
1 /** 2 * 子线程中处理拷贝数据,耗时操作 3 */ 4 private Thread thread = new Thread(new Runnable() { 5 @Override 6 public void run() { 7 boolean isSDPresent = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED); 8 if (isSDPresent) { 9 File sdCardDir = Environment.getExternalStorageDirectory(); 10 File myDir = new File(sdCardDir, "/double_ball/");//创建文件夹 11 if (!myDir.exists()) { 12 myDir.mkdir(); 13 System.out.println("~~~~~~~~~>>>>file path: " + myDir.getPath()); 14 AssetCopyUtils.copyAssetFileToSDCard(MainActivity.this, "ball.db", myDir.getAbsolutePath() + "/double.db", new AssetCopyUtils.ICopyProgressCallBack() { 15 @Override 16 public void onProgress(int progress) { 17 if (progress == 100) { 18 mHandler.sendEmptyMessage(1001); 19 hideLoading(); 20 } 21 } 22 }); 23 } else { 24 mHandler.sendEmptyMessage(1002); 25 hideLoading(); 26 } 27 } else { 28 mHandler.sendEmptyMessage(1003); 29 hideLoading(); 30 } 31 } 32 });
安卓10以上存储:
1 private Thread newThread = new Thread(new Runnable() { 2 @Override 3 public void run() { 4 String dirPath = getExternalCacheDir() + "/double_file/"; 5 File filePath = new File(dirPath + File.separator); 6 System.out.println("~~~~~~~~~~~path:" + filePath.getAbsolutePath()); 7 if (!filePath.exists()) { 8 filePath.mkdir(); 9 AssetCopyUtils.copyAssetFileToSDCard(MainActivity.this, "ball.db", filePath.getAbsolutePath() + "/double.db", new AssetCopyUtils.ICopyProgressCallBack() { 10 @Override 11 public void onProgress(int progress) { 12 if (progress == 100) { 13 mHandler.sendEmptyMessage(1001); 14 hideLoading(); 15 } 16 } 17 }); 18 } else { 19 mHandler.sendEmptyMessage(1002); 20 hideLoading(); 21 } 22 } 23 });
【调用】
1 @Override 2 public void onClick(View v) { 3 boolean isHas = PermissionUtil.checkPermission(MainActivity.this, PERMISSION_STORAGE, PERMISSION_STORAGE_CODE); 4 if (isHas) { 5 showLoading("服务端数据同步中..."); 6 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { 7 newThread.start(); 8 } else { 9 thread.start(); 10 } 11 } else { 12 ToastUtil.getInstance().showToast("您还没有权限哦!"); 13 } 14 }