編輯:關於Android編程
今天修復一個使用系統的方法 android.webkit.URLUtil.guessFileName(String url,String contentDisposition, String mimeType) 方法獲取文件名。
傳入參數如圖:
問題來了,guessFileName 返回的文件名不是 contentDisposition 中給出的fileName ,而是 qujing-fZ喎?/kf/ware/vc/" target="_blank" class="keylink">vci1hbmRyb2lkLmJpbjwvc3Ryb25nPjxiciAvPg0Kv7TBy8/CVVJMVXRpbCC1xNS0wus8L3A+DQo8cHJlIGNsYXNzPQ=="brush:java;">
URLUtil源碼地址 問題最終指向了 MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType); 這個方法,是系統保存的一個MIMETYPE 的HashMap ,根據傳入的Key,返回對應的文件類型 這裡列舉了一部分。 但是注意了,我們傳入的文件類型 mimetype = application/otcstream ,系統中是沒有這個的。 The content-type should be whatever it is known to be, if you know it. application/octet-stream is defined as “arbitrary binary data” in RFC 2046, and there’s a definite overlap here of it being appropriate for entities whose sole intended purpose is to be saved to disk, and from that point on be outside of anything “webby”. Or to look at it from another direction; the only thing one can safely do with application/octet-stream is to save it to file and hope someone else knows what it’s for. You can combine the use of Content-Disposition with other content-types, such as image/png or even text/html to indicate you want saving rather than display. It used to be the case that some browsers would ignore it in the case of text/html but I think this was some long time ago at this point (and I’m going to bed soon so I’m not going to start testing a whole bunch of browsers right now; maybe later). RFC 2616 also mentions the possibility of extension tokens, and these days most browsers recognise inline to mean you do want the entity displayed if possible (that is, if it’s a type the browser knows how to display, otherwise it’s got no choice in the matter). This is of course the default behaviour anyway, but it means that you can include the filename part of the header, which browsers will use (perhaps with some adjustment so file-extensions match local system norms for the content-type in question, perhaps not) as the suggestion if the user tries to save. Hence: Content-Type: application/octet-stream Content-Type: image/png Content-Type: image/png Of those browsers that recognise inline some would always use it, while others would use it if the user had selected “save link as” but not if they’d selected “save” while viewing (or at least IE used to be like that, it may have changed some years ago). 這是StackOverFlower 上對 該類型的介紹,原地址在這裡 最後,說下問題解決方法: MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType)傳入的參數使用 typeFromExt 替換。public static final String guessFileName(
String url,
String contentDisposition,
String mimeType) {
String filename = null;
String extension = null;
// If we couldn't do anything with the hint, move toward the content disposition
if (filename == null && contentDisposition != null) {
filename = parseContentDisposition(contentDisposition);
if (filename != null) {
int index = filename.lastIndexOf('/') + 1;
if (index > 0) {
filename = filename.substring(index);
}
}
}
// If all the other http-related approaches failed, use the plain uri
if (filename == null) {
String decodedUrl = Uri.decode(url);
if (decodedUrl != null) {
int queryIndex = decodedUrl.indexOf('?');
// If there is a query string strip it, same as desktop browsers
if (queryIndex > 0) {
decodedUrl = decodedUrl.substring(0, queryIndex);
}
if (!decodedUrl.endsWith("/")) {
int index = decodedUrl.lastIndexOf('/') + 1;
if (index > 0) {
filename = decodedUrl.substring(index);
}
}
}
}
// Finally, if couldn't get filename from URI, get a generic filename
if (filename == null) {
filename = "downloadfile";
}
// Split filename between base and extension
// Add an extension if filename does not have one
int dotIndex = filename.indexOf('.');
if (dotIndex < 0) {
if (mimeType != null) {
extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType);
if (extension != null) {
extension = "." + extension;
}
}
if (extension == null) {
if (mimeType != null && mimeType.toLowerCase(Locale.ROOT).startsWith("text/")) {
if (mimeType.equalsIgnoreCase("text/html")) {
extension = ".html";
} else {
extension = ".txt";
}
} else {
extension = ".bin";
}
}
} else {
if (mimeType != null) {
// Compare the last segment of the extension against the mime type.
// If there's a mismatch, discard the entire extension.
int lastDotIndex = filename.lastIndexOf('.');
String typeFromExt = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
filename.substring(lastDotIndex + 1));
if (typeFromExt != null && !typeFromExt.equalsIgnoreCase(mimeType)) {
//這裡是判斷後綴名,有問題
extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType);
if (extension != null) {
extension = "." + extension;
}
}
}
if (extension == null) {
extension = filename.substring(dotIndex);
}
filename = filename.substring(0, dotIndex);
}
return filename + extension;
}
add("application/pgp-signature", "pgp");
add("application/pics-rules", "prf");
add("application/pkix-cert", "cer");
add("application/rar", "rar");
add("application/rdf+xml", "rdf");
add("application/rss+xml", "rss");
add("application/zip", "zip");
add("application/vnd.android.package-archive", "apk");
add("application/vnd.cinderella", "cdy");
add("application/vnd.ms-pki.stl", "stl");
Content-Disposition: attachment; filename=”picture.png”
Means “I don’t know what the hell this is. Please save it as a file, preferably named picture.png”.
Content-Disposition: attachment; filename=”picture.png”
Means “This is a PNG image. Please save it as a file, preferably named picture.png”.
Content-Disposition: inline; filename=”picture.png”
Means “This is a PNG image. Please display it unless you don’t know how to display PNG images. Otherwise, or if the user chooses to save it, we recommend the name picture.png for the file you save it as”.
Toast英文名為土司,在Android裡面這個類是用來彈出提示信息的,我想sdk作者是認為提示信息片長得就像一塊土司吧。這個理論就不多說什麼了,開始我們的實踐。 第一步
今天你被耍流氓了嗎?微信聊天時有沒有收到“XX撤回一條消息並親了你一下”的消息,在朋友圈中一排排“送你一朵小花&rdqu
當要顯示的數據過多時,為了更好的提升用戶感知,在很多APP中都會使用分頁刷新顯示,比如浏覽新聞,向下滑動到當前ListView的最後一條信息(item)時,會提示刷新加載
首先我們來回憶一下傳統用Activity進行的頁面切換,activity之間切換,首先需要新建intent對象,給該對象設置一些必須的參數,然後調用startActivi