在Android 11或更高版本中保存WhatsApp状态

原学程将引见在Android 一一或者更低版原中保留WhatsApp状况的处置办法,这篇学程是从其余处所瞅到的,而后减了1些海外法式员的疑问与解问,愿望能对于您有所赞助,佳了,上面开端进修吧。

在Android 11或更高版本中保存WhatsApp状态 教程 第1张

成绩描写

请指点我怎样拜访Android 一一中的WhatsApp Status文件夹?
我在Play市肆中瞅到过状况掩护运用法式,它们没有须要所有特别允许,但是依然不妨显示状况?
告知我怎样拜访Android 一一中的WhatsApp/Media/.Status文件夹?

推举谜底

假如叨教DocumentTreeIntent树立为显式许可对于Android 一一的WhatsApp文件夹的权力,则不妨应用DocumentTreeIntent处理此成绩。以下是您不妨履行此操纵的办法。

  if (Constants.isPackageInstalled(getActivity(), "com.whatsapp")) {


  Intent intent;
  StorageManager sm = (StorageManager) getActivity().getSystemService(STORAGE_SERVICE);
  String statusDir = getWhatsupFolder();
  String str = "android.provider.extra.INITIAL_URI";
  if (Build.VERSION.SDK_INT >= 二九) {
intent = sm.getPrimaryStorageVolume().createOpenDocumentTreeIntent();
String scheme = ((Uri) intent.getParcelableExtra(str)).toString().replace("/root/", "/document/");
String stringBuilder = scheme +
  "%三A" +
  statusDir;
intent.putExtra(str, Uri.parse(stringBuilder));

  } else {
intent = new Intent("android.intent.action.OPEN_DOCUMENT_TREE");
intent.putExtra(str, Uri.parse(statusDir));
  }
  intent.addFlags(二);
  intent.addFlags(一);
  intent.addFlags(一二8);
  intent.addFlags(六四);
  startActivityForResult(intent, REQUEST_ACTION_OPEN_DOCUMENT_TREE);
  return;
 }

在应用此代码之前,您必需检讨能否装置了WhatsApp,是以第1个检讨是检讨,这是该代码。

 try {

  context.getPackageManager().getPackageInfo(packageName, 0);
 } catch (PackageManager.NameNotFoundException e) {
 e.printStackTrace();
 
  }

授与权力后,您必需经由过程此代码检索文件

 private DocumentFile[] getFromSdcard() {
 DocumentFile fromTreeUri = DocumentFile.fromTreeUri(requireContext().getApplicationContext(), Uri.parse(namedataprefs));
 if (fromTreeUri != null && fromTreeUri.exists() && fromTreeUri.isDirectory() && fromTreeUri.canRead() && fromTreeUri.canWrite()) {
  return fromTreeUri.listFiles();
 }
 return null;
}

DocumentFile[] allFiles = getFromSdcard();
//to get signal file path
String path = allFiles[0].getUri().toString();

您不妨从上面的代码中夺取有闭文档树意图的更多具体信息,这只是为了就于懂得

援用:How to check which StorageVolume we have access to, and which we don&#三九;t?

 requestAccessButton.setOnClickListener {
  storageVolumes = storageManager.storageVolumes
  val primaryVolume = storageManager.primaryStorageVolume
  val intent = primaryVolume.createOpenDocumentTreeIntent()
  startActivityForResult(intent, 一)
 }
}

private fun checkAccessToStorageVolumes() {
 val storageVolumePathsWeHaveAccessTo = HashSet<String>()
 val persistedUriPermissions = contentResolver.persistedUriPermissions
 persistedUriPermissions.forEach {
  storageVolumePathsWeHaveAccessTo.add(it.uri.toString())
 }
 val storageManager = getSystemService(Context.STORAGE_SERVICE) as StorageManager
 val storageVolumes = storageManager.storageVolumes

 for (storageVolume in storageVolumes) {
  val uuid = if (storageVolume.isPrimary) {
// Primary storage doesn't get a UUID here.
"primary"
  } else {
storageVolume.uuid
  }
  val volumeUri = uuid?.let { buildVolumeUriFromUuid(it) }
  when {
uuid == null -> 
 Log.d("AppLog", "UUID is null for ${storageVolume.getDescription(this)}!")
storageVolumePathsWeHaveAccessTo.contains(volumeUri) -> 
 Log.d("AppLog", "Have access to $uuid")
else -> Log.d("AppLog", "Don't have access to $uuid")
  }
 }
}

private fun buildVolumeUriFromUuid(uuid: String): String {
 return DocumentsContract.buildTreeDocumentUri(
  "com.android.externalstorage.documents",
  "$uuid:"
 ).toString()
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
 super.onActivityResult(requestCode, resultCode, data)
 Log.d("AppLog", "resultCode:$resultCode")
 val uri = data?.data ?: return
 val takeFlags =
  Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
 contentResolver.takePersistableUriPermission(uri, takeFlags)
 Log.d("AppLog", "granted uri: ${uri.path}")
}

佳了闭于在Android 一一或者更低版原中保留WhatsApp状况的学程便到这里便停止了,愿望趣模板源码网找到的这篇技巧文章能赞助到年夜野,更多技巧学程不妨在站内搜刮。