android 获取资源绝对路径,Android 获取文件真实路径

public class GetRealPath {

public static String getFPUriToPath(Context context, Uri uri) {

try {

List packs = context.getPackageManager().getInstalledPackages(PackageManager.GET_PROVIDERS);

if (packs != null) {

String fileProviderClassName = FileProvider.class.getName();

for (PackageInfo pack : packs) {

ProviderInfo[] providers = pack.providers;

if (providers != null) {

for (ProviderInfo provider : providers) {

if (uri.getAuthority().equals(provider.authority)) {

if (provider.name.equalsIgnoreCase(fileProviderClassName)) {

Class fileProviderClass = FileProvider.class;

try {

Method getPathStrategy = fileProviderClass.getDeclaredMethod("getPathStrategy", Context.class, String.class);

getPathStrategy.setAccessible(true);

Object invoke = getPathStrategy.invoke(null, context, uri.getAuthority());

if (invoke != null) {

String PathStrategyStringClass = FileProvider.class.getName() + "$PathStrategy";

Class> PathStrategy = Class.forName(PathStrategyStringClass);

Method getFileForUri = PathStrategy.getDeclaredMethod("getFileForUri", Uri.class);

getFileForUri.setAccessible(true);

Object invoke1 = getFileForUri.invoke(invoke, uri);

if (invoke1 instanceof File) {

String filePath = ((File) invoke1).getAbsolutePath();

return replace(filePath);

}

}

} catch (NoSuchMethodException e) {

e.printStackTrace();

} catch (InvocationTargetException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

break;

}

break;

}

}

}

}

}

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

public static String replace(String filePath) {

if (filePath.contains("%")) {

filePath = filePath.replace("%", "%25");

}

if (filePath.contains("#")) {

filePath = filePath.replace("#", "%23");

}

if (filePath.contains("&")) {

filePath = filePath.replace("&", "%26");

}

if (filePath.contains("?")) {

filePath = filePath.replace("?", "%3F");

}

return filePath;

}

/**

* 查询内容解析器,找到文件存储地址

*

ef: android中转换content://media/external/images/media/539163为/storage/emulated/0/DCIM/Camera/IMG_20160807_123123.jpg

*

把content://media/external/images/media/X转换为file:///storage/sdcard0/Pictures/X.jpg

* @param context

* @param contentUri

* @return

*/

public static String getRealPathFromUri(Context context, Uri contentUri) {

Cursor cursor = null;

try {

String[] proj = { MediaStore.Images.Media.DATA };

cursor = context.getContentResolver().query(contentUri, proj, null, null, null);

if (cursor != null && cursor.getColumnCount() > 0) {

cursor.moveToFirst();

int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

String path = cursor.getString(column_index);

return path;

} else {

}

} catch (Exception e) {

} finally {

if (cursor != null) {

cursor.close();

}

}

return "";

}

/**

* 获取完整文件名(包含扩展名)

* @param filePath

* @return

*/

public static String getFilenameWithExtension(String filePath) {

if (filePath == null || filePath.length() == 0) {

return "";

}

int lastIndex = filePath.lastIndexOf(File.separator);

String filename = filePath.substring(lastIndex + 1);

return filename;

}

/**

* 判断文件路径的文件名是否存在文件扩展名 eg: /external/images/media/2283

* @param filePath

* @return

*/

public static boolean isFilePathWithExtension(String filePath) {

String filename = getFilenameWithExtension(filePath);

return filename.contains(".");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值