android 本地地址转换为url,android本地mipmap图片转url、绝对路径转URL URL URI File Path 转换...
标签: url uri file path
File to URI:
File file = ...;
URI uri = file.toURI();
File to URL:
File file = ...;
URL url = file.toURI().URL();
URL to File:
URL url = ...;
File file = new Path(url.getPath()).toFile();
URI to URL:
URI uri = ...;
URL url = uri.toURL();
URL to URI:
URL url = ...;
URI uri = url.toURI();
一般情况下采用上述方式都可以安全的使用.
但是, 当处理本地路径且有空格,或者特殊字符,比如汉字等. 路径在相互的转换过程中, 可能会出现转换的无效字符错误异常.
所以, 可以使用Eclipse提供的工具类org.eclipse.core.runtime.URIUtil (插件: org.eclipse.equinox.simpleconfigurator)来进行转换.
URL URI File Path 转换(原创)
比如URL to File:
URL url = ...;
File file = URIUtil.toFile(URIUtil.toURI(url));
当URL, URI直接互相转换时,也可以使用该URIUtil工具类.
toURI
toURL
还有一个工具类,就是org.eclipse.core.runtime.FileLocator(插件: org.eclipse.equinox.common) 也可以对URL进行File的格式化. 比如toFileURL方法.
附源码:
package org.eclipse.equinox.internal.simpleconfigurator.utils;
import java.io.File;
import java.net.*;
public class URIUtil {
private static final String SCHEME_FILE = "file"; //$NON-NLS-1$
private static final String UNC_PREFIX = "//"; //$NON-NLS-1$
public static URI append(URI base, String extension) {
try {
String path = base.getPath();
if (path == null)
return appendOpaque(base, extension);
//if the base is already a directory then resolve will just do the right thing
if (path.endsWith("/")) {//$NON-NLS-1$
URI result = base.resolve(extension);
//Fix UNC paths that are incorrectly normalized by URI#resolve (see Java bug 4723726)
String resultPath = result.getPath();
if (path.startsWith(UNC_PREFIX) && (resultPath == null || !resultPath.startsWith(UNC_PREFIX)))
result = new URI(result.getScheme(), "///" + result.getSchemeSpecificPart(), result.getFragment()); //$NON-NLS-1$
return result;
}
path = path + "/" + extension; //$NON-NLS-1$
return new URI(base.getScheme(), base.getUserInfo(), base.getHost(), base.getPort(), path, base.getQuery(), base.getFragment());
} catch (URISyntaxException e) {
//shouldn't happen because we started from a valid URI
throw new RuntimeException(e);
}
}
private static URI appendOpaque(URI base, String extension) throws URISyntaxException {
String ssp = base.getSchemeSpecificPart();
if (ssp.endsWith("/")) //$NON-NLS-1$
ssp += extension;
else
ssp = ssp + "/" + extension; //$NON-NLS-1$
return new URI(base.getScheme(), ssp, base.getFragment());
}
public static URI fromString(String uriString) throws URISyntaxException {
int colon = uriString.indexOf(':');
int hash = uriString.lastIndexOf('#');
boolean noHash = hash < 0;
if (noHash)
hash = uriString.length();
String scheme = colon < 0 ? null : uriString.substring(0, colon);
String ssp = uriString.substring(colon + 1, hash);
String fragment = noHash ? null : uriString.substring(hash + 1);
//use java.io.File for constructing file: URIs
if (scheme != null && scheme.equals(SCHEME_FILE)) {
File file = new File(uriString.substring(5));
if (file.isAbsolute())
return file.toURI();
scheme = null;
if (File.separatorChar != '/')
ssp = ssp.replace(File.separatorChar, '/');
}
return new URI(scheme, ssp, fragment);
}
public static boolean sameURI(URI url1, URI url2) {
if (url1 == url2)
return true;
if (url1 == null || url2 == null)
return false;
if (url1.equals(url2))
return true;
if (url1.isAbsolute() != url2.isAbsolute())
return false;
// check if we have two local file references that are case variants
File file1 = toFile(url1);
return file1 == null ? false : file1.equals(toFile(url2));
}
public static File toFile(URI uri) {
try {
if (!SCHEME_FILE.equalsIgnoreCase(uri.getScheme()))
return null;
//assume all illegal characters have been properly encoded, so use URI class to unencode
return new File(uri);
} catch (IllegalArgumentException e) {
//File constructor does not support non-hierarchical URI
String path = uri.getPath();
//path is null for non-hierarchical URI such as file:c:/tmp
if (path == null)
path = uri.getSchemeSpecificPart();
return new File(path);
}
}
public static String toUnencodedString(URI uri) {
StringBuffer result = new StringBuffer();
String scheme = uri.getScheme();
if (scheme != null)
result.append(scheme).append(':');
//there is always a ssp
result.append(uri.getSchemeSpecificPart());
String fragment = uri.getFragment();
if (fragment != null)
result.append('#').append(fragment);
return result.toString();
}
public static URI toURI(URL url) throws URISyntaxException {
//URL behaves differently across platforms so for file: URLs we parse from string form
if (SCHEME_FILE.equals(url.getProtocol())) {
String pathString = url.toExternalForm().substring(5);
//ensure there is a leading slash to handle common malformed URLs such as file:c:/tmp
if (pathString.indexOf('/') != 0)
pathString = '/' + pathString;
else if (pathString.startsWith(UNC_PREFIX) && !pathString.startsWith(UNC_PREFIX, 2)) {
//URL encodes UNC path with two slashes, but URI uses four (see bug 207103)
pathString = UNC_PREFIX + pathString;
}
return new URI(SCHEME_FILE, null, pathString, null);
}
try {
return new URI(url.toExternalForm());
} catch (URISyntaxException e) {
//try multi-argument URI constructor to perform encoding
return new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
}
}
public static URL toURL(URI uri) throws MalformedURLException {
return new URL(uri.toString());
}
}
android 本地地址转换为url,android本地mipmap图片转url、绝对路径转URL URL URI File Path 转换...相关推荐
- android 本地地址转换为url,安卓 File和url之间的转换
转自:http://blog.sina.com.cn/s/blog_437ff56b0101dtrf.html File to URI: File file = ...; URIuri= file.t ...
- Android的webview加载本地html、本apk内html和远程URL
-----打开本包内asset目录下的index.html文件 //wView.loadUrl(" file:///android_asset/index.html "); --- ...
- Android中Uri 和Path之间的相互转化
Android Uri to Path 现在遇到的常规Uri有两种: 媒体文件的Uri是content://, 表示这是一个数据库数据.去数据库查询正常返回. 其他的文件Uri是file://, 表示 ...
- android 获取drawable 对象,Android 实现将本地资源图片转换成Drawable的方法
Android 实现将本地资源图片转换成Drawable的方法 发布时间:2020-11-06 16:37:09 来源:亿速云 阅读:255 作者:Leah 这篇文章将为大家详细讲解有关Android ...
- Android中使用PhotoView和ViewPager查看图片,并保存到本地
如下图:可以滑动查看,可以放大缩小,点击长按保存图片 build.gradle中添加依赖 compile 'com.github.chrisbanes.photoview:library:+' ...
- Android获取本地相册图片
Android获取本地相册图片 第一步设置静态权限 <uses-permission android:name="android.permission.WRITE_EXTERNAL_S ...
- LocalResizeIMG前端HTML5本地压缩图片上传,兼容移动设备IOS,android
LocalResizeIMG前端HTML5本地压缩图片上传,兼容移动设备IOS,android jincon 发表于 2015-02-26 18:31:01 发表在: php开发 localresiz ...
- Android 使用ContentProvider扫描手机中的图片,仿微信显示本地图片效果
转载请注明本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/18730223),请尊重他人的辛勤劳动成果,谢谢! 写这篇文 ...
- Android之本地相册图片选取和拍照以及图片剪辑
转载请注明出处:http://blog.csdn.net/loveyaozu/article/details/51160482 相信有很多Android开发人员在日常开发中,由于项目需求,需要我们的A ...
最新文章
- 网络入侵检测规避工具fragrouter
- 数据结构--二叉树的创建和相关操作
- 000-SQL Server
- CLR via C# (二)
- ArcGIS实验教程——实验三十六:ArcGIS Python脚本的巧妙使用
- python类继承返回值_python继承threading.Thread实现有返回值的子类实例
- 阿里云 EMR Delta Lake 在流利说数据接入中的架构和实践
- 小P寻宝记——好基友一起走
- 快应用实现网络测速功能_网络阅卷系统应用系统功能实现情况
- 数据库概述之数据库设计实例分析
- python批量生成姓名_Python 批量生成中文姓名(百家姓)
- [渝粤教育] 西南科技大学 刑法学 在线考试复习资料
- ACPI\VEN_LENDEV_0078[未知设备]
- 计算机ps图片在哪里看,怎么看图片有没有PS 两种查看照片有没被PS过的方法-电脑教程...
- Win10打开“此电脑”读绿条,显示“正在处理”
- 【IOI2020国家集训队作业 Part 1】CF505E Mr. Kitayuta vs. Bamboos
- java计算机毕业设计化妆品销售网站源码+mysql数据库+系统+lw文档+部署
- 百度地图绘制工具类 DrawingManager.js 源码
- microk8s helm2 Error: no available release name found
- 交货状态html什么意思,Marc Jacobs The Uptown 手提包
热门文章
- python多线程爬取多个网页_python多线程爬取网页
- 1101: 逆序数字(函数专题)
- 闲暇所学“表白对话框”
- matlab中如何调用gpu进行并行计算_极致安卓-Termux/Aid learning开启WebGL手机GPU并行计算...
- 第2章线性表的基本使用及其cpp示例(第二章汇总,线性表都在这里)
- 2019年蓝桥杯第一题
- 开源内容管理系统 php mysql_十大免费PHP+MySql平台内容管理系统推荐
- python 日志不会按照日期分割_django实现日志按日期分割
- conda失败说没有写权限_爱情中,为什么男生表白失败,女生还说可以继续做朋友,想过没有...
- linux和windows和鸿蒙,linux很好,但为何大家都用Win,鸿蒙系统站错阵营了吗?