- private String getRealPath(Uri fileUrl){
String fileName = null;
Uri filePathUri = fileUrl;
if(fileUrl!= null){
if (fileUrl.getScheme().toString().compareTo("content")==0)
//content: 开头的uri
{
Cursor cursor = MainActivity.this.getContentResolver().query(filePathUri, null, null,
null, null);
if (cursor != null && cursor.moveToFirst())
{
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME);
//可能参数为MediaStroe.Images.Media.DATA;根据手机目录不同尝试不同参数
fileName = cursor.getString(column_index); //取出文件路径
if(!fileName.startsWith("/mnt")){ //检查是否有”/mnt“前缀
fileName = "/mnt" + fileName;
}
cursor.close();
}
}else if (filePathUri.getScheme().compareTo("file")==0) //file:///开头的uri
{
fileName = filePathUri.toString();
fileName = filePathUri.toString().replace("file://", ""); //替换file://
if(!fileName.startsWith("/mnt")){ //加上"/mnt"头
fileName += "/mnt";
}
}
}
return fileName;
}
根据Url获取手机文件真实路径
最新推荐文章于 2023-02-03 16:19:07 发布