简体   繁体   English

我可以硬编码`file://`uri前缀吗?

[英]Is the `file://` uri prefix something I can hardcode?

I was wondering if I can hardcore the file:// prefix into one of my functions in android. 我想知道是否可以将file://前缀添加到android中的其中一个函数中。

The function is supposed to determine whether or not the given link points to an external resource on the web, or an internal resource inside the phone itself 该功能应确定给定的链接是否指向网络上的外部资源,或电话本身内部的内部资源。

public Uri generate_image_uri(String link)
{
    // link can be "1DCHiI2.jpg"
    // link can be "file://smiley_face.jpg"

    if (!link.startsWith("file://")
       return Uri.parse("https://i.imgur.com/" + link);
    else
       return Uri.parse(link);
}

Is this advisable? 这是明智的吗? Or is there a more "fault tolerant" way of getting file:// ? 还是有一种更“容错”的方式来获取file:// maybe some function like getProperFilePrefixForThisAndroidVersion(); 也许像getProperFilePrefixForThisAndroidVersion();这样的函数getProperFilePrefixForThisAndroidVersion(); ?


In order to clarify my question: 为了澄清我的问题:

given the following code 给定以下代码

(new File(getFilesDir(), "hello_world.jpg")).toString();

Is it safe to assume within reasonable probability that the resulting string will always start with file:// in all current and future Android versions? 是否可以合理地假设,在所有当前和将来的Android版本中,结果字符串始终以file://开头?

Given: 鉴于:

 (new File(getFilesDir(), "hello_world.jpg")).toString(); 

is it safe to assume within reasonable probability that the resulting string will always start with file:// in all current and future Android versions? 是否可以合理地假设,在所有当前和将来的Android版本中,结果字符串始终以file://开头?

No. 没有。

According to the javadoc for File on Android, File.toString returns: 根据Android上File的javadoc,File.toString返回:

"... the pathname string of this abstract pathname. This is just the string returned by the getPath() method." “ ...此抽象路径名的路径名字符串。这只是getPath()方法返回的字符串。”

Not a "file://" URL. 不是“ file://” URL。

If you want to get a properly formed "file://" URL, do this: 如果要获取格式正确的“ file://” URL,请执行以下操作:

 new File(...).toURI().toString()

Now, technically the protocol for a URL (ie "file") is case insensitive: 现在,从技术上讲 ,URL(即“文件”)的协议不区分大小写:

Which means that "FILE://" or "File://" etc are technically valid alternatives. 这意味着“ FILE://”或“ File://”等在技术上有效。

However, the probability that above expression would ever emit anything other than the lower-case form of the protocol is (um) vanishingly small 1 . 然而,上面的表达式将永远发射以外的任何其他协议的小写形式的概率为(UM)微乎其微1。


1 - It would entail monumentally stupid decision making by a number of people. 1-这将导致许多人做出愚蠢的决策。 And they are NOT stupid people. 他们不是愚蠢的人。

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

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