简体   繁体   English

检查 uri 是否来自可移动存储

[英]check if a uri is from removable storage

how to check if a Uri user selected from action_open_document tree is got from removable Sd card?如何检查从 action_open_document 树中选择的 Uri 用户是否来自可移动 Sd 卡? i check this but its same for primary sd card and removable sd card!我检查了这一点,但对于主 SD 卡和可移动 SD 卡也是如此! is there any other way?还有其他方法吗?

   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

     String id=DocumentsContract.getTreeDocumentId(uri);

                    Uri mainuri=DocumentsContract.buildDocumentUriUsingTree(uri,id);


                    grantUriPermission(G.context.getPackageName(), uri,   Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

          if(   "com.android.externalstorage.documents".equals(uri.getAuthority())){

// its return true for primary and removable sd card !!


}

Since android Q, you must use SAF.从android Q开始,你必须使用SAF。 In order to know if a URI is a removable media, you can try using the path string: if you find a string like "HHHH-HHHH:" (where H = hexadecimal string character) in the uri.getPath() it means that is a removable media.为了知道 URI 是否是可移动媒体,您可以尝试使用路径字符串:如果您在 uri.getPath() 中找到类似“HHHH-HHHH:”(其中 H = 十六进制字符串字符)的字符串,则表示是可移动媒体。

 /**
 * Check if SAF uri point to a removable media
 * Search for this regular expression:
 * ".*\\b[ABCDFE[0-9]][ABCDFE[0-9]][ABCDFE[0-9]][ABCDFE[0-9]]-[ABCDFE[0-9]][ABCDFE[0-9]][ABCDFE[0-9]][ABCDFE[0-9]]:\\b.*"
 * @param uri: SAF URI
 * @return true if removable, false if is internal
 */
public boolean isRemovable (Uri uri) {
    String path = uri.getPath();
    String r1 = "[ABCDEF[0-9]]";
    String r2 = r1 + r1 + r1 + r1;
    String regex = ".*\\b" + r2 + "-" + r2 + ":\\b.*";
    if (path != null) {
        return path.matches(regex);
    } else return false;
}

The last method use less memory.最后一种方法使用较少的内存。 The following method is faster nut consume more memory due the regex string, but is short and faster:以下方法更快,由于正则表达式字符串消耗更多内存,但更短且更快:

public boolean isRemovable (Uri uri) {
    String path = uri.getPath();
    if (path != null) {
        return path.matches(".*\\b[ABCDFE[0-9]][ABCDFE[0-9]][ABCDFE[0-9]][ABCDFE[0-9]]-[ABCDFE[0-9]][ABCDFE[0-9]][ABCDFE[0-9]][ABCDFE[0-9]]:\\b.*");
    } else return false;
}

UPDATE: The original regex only works for subfolder on the SDCARD.更新:原始正则表达式仅适用于 SDCARD 上的子文件夹。 To include the root directory, delete the last '\\d' characters.要包含根目录,请删除最后一个 '\\d' 字符。 This is the correct REGEX:这是正确的正则表达式:

".*\\b[ABCDFE[0-9]][ABCDFE[0-9]][ABCDFE[0-9]][ABCDFE[0-9]]-[ABCDFE[0-9]][ABCDFE[0-9]][ABCDFE[0-9]][ABCDFE[0-9]]:.*"

so the correct function would be:所以正确的功能是:

private boolean isRemovable (Uri uri) {
    String path = uri.getPath();
    String r1 = "[ABCDEF[0-9]]";
    String r2 = r1 + r1 + r1 + r1;
    String regex = ".*\\b" + r2 + "-" + r2 + ":.*";
    if (path != null) {
        return path.matches(regex);
    } else return false;
}

No.没有。

There is no requirement that a Uri from a storage provider be identifiable in any way.不要求以任何方式识别来自存储提供者的Uri Your assumption (that the authority is com.android.externalstorage.documents for a certain storage provider) does not have to be correct on any Android device.您的假设(某个存储提供商的权限是com.android.externalstorage.documents )在任何 Android 设备上都不一定正确。 Device manufacturers can supply their own storage providers, with their own Uri structures.设备制造商可以提供他们自己的存储提供者,以及他们自己的Uri结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM