- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在使用此函数从 uri 获取图像路径:
private 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);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
上述功能适用于 < 23 版本。但它用 android marshmallow 返回 null ?
I am using emulator nexus 4 with android 6.0 x86_64
模拟器有存储权限。
可能是什么问题?
最佳答案
请引用this post关于在 android 中处理 Uri 以支持最新版本的 android 操作系统和新设备,您的大部分要求都可以通过使用 streams
而不是获取文件系统中 Assets 的路径来满足。所以请考虑使用 streams
。
/**
* Get a file path from a Uri. This will get the the path for Storage Access
* Framework Documents, as well as the _data field for the MediaStore and
* other file-based ContentProviders.
*
* @param context The context.
* @param uri The Uri to query.
* @author paulburke
*/
public static String getPath(final Context context, final Uri uri) {
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
// DocumentProvider
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
// ExternalStorageProvider
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
}
// TODO handle non-primary volumes
}
// DownloadsProvider
else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
}
// MediaProvider
else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String[] selectionArgs = new String[] {
split[1]
};
return getDataColumn(context, contentUri, selection, selectionArgs);
}
}
// MediaStore (and general)
else if ("content".equalsIgnoreCase(uri.getScheme())) {
// Return the remote address
if (isGooglePhotosUri(uri))
return uri.getLastPathSegment();
return getDataColumn(context, uri, null, null);
}
// File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
/**
* Get the value of the data column for this Uri. This is useful for
* MediaStore Uris, and other file-based ContentProviders.
*
* @param context The context.
* @param uri The Uri to query.
* @param selection (Optional) Filter used in the query.
* @param selectionArgs (Optional) Selection arguments used in the query.
* @return The value of the _data column, which is typically a file path.
*/
public static String getDataColumn(Context context, Uri uri, String selection,
String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {
column
};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
null);
if (cursor != null && cursor.moveToFirst()) {
final int index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is ExternalStorageProvider.
*/
public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is DownloadsProvider.
*/
public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is MediaProvider.
*/
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is Google Photos.
*/
public static boolean isGooglePhotosUri(Uri uri) {
return "com.google.android.apps.photos.content".equals(uri.getAuthority());
}
取自here从这个answer .这是一个丑陋的 hack,不能保证在所有设备上工作,但根据我的经验,它可以在 99% 的设备上工作。根据 CommonsWare,您不应该假设您通过 ContentResolver 获得的流可以转换为文件。相反,您应该使用 context.getContentResolver().openInputStream(fileUri)
方法从该 Uri
打开一个 inputstream
。引用他的commonsBlog post有关此以及如何处理 Uris 的更多信息
关于android - 在 android 棉花糖上获取 realPath 返回 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33208911/
出于性能原因,我们应该只使用 realpath()而不是 realpath() + file_exists()在检查文件或目录是否存在时 ?? 案例 A:: if(realpath(__DIR__."
做这样的事情是不是一种感觉 $dir = realpath(dirname(__FILE__)); // or for php 5.3+: $dir = realpath(__DIR__) 或者
调用 Unix realpath() 函数的传统方法是 realpath(pathname, buf) 其中 buf 是用户提供的PATH_MAX 字节空间的缓冲区。这是有问题的,因为 PATH_MA
这个问题在这里已经有了答案: Unexpected behavior in printing a char array in C (1 个回答) 关闭 8 年前。 这是我的功能,它在给定目录中查找常
PHP 的 realpath() 在 manual 中有详细描述,我只是想了解它在哪些场景下有用。因为看起来我的代码可以很好地处理 ../../path 这样的路径,所以我不确定 realpath()
我使用 MediaWiki,但出现错误。我在文件中找到了它。有一行代码: $IP = realpath( '.' ); 但是 $IP 返回一个空字符串,所以系统中文件的路径不起作用。 (例如,如果 $
我想检查一个路径是否是另一个路径的子路径,所以我调用 realpath(file_path, NULL) 并将结果与 strncmp 进行比较。 我写这个主题的原因是:realpath 设置err
我在使用 realpath 函数时遇到了一个奇怪的问题。该函数在给定一个作为程序参数接收的字符串时工作,但在给定我在源代码中定义的字符串时失败。这是一个简单的程序: #include #includ
我正在开发一个程序,它使用 realpath() 来获取文件的绝对路径。不幸的是,这个函数需要一个字符串缓冲区,这个缓冲区应该很大,以至于它足够大,当这个应用程序必须跨多个平台运行时,这是不安全的。是
给定 现代 Linux/UNIX/OSX(带 realpath) bash 4+(甚至在 OSX 上) 是 "$PWD" == "$(realpath .)" 总是正确的? 最佳答案 很容易测试情况并
我的 Windows 服务器上有一个符号链接(symbolic link),它是这样制作的: F:\>mkdir link-target F:\>mklink /D link f:\link-targ
当我编译以下代码时: #define _POSIX_C_SOURCE 200112L #define _ISOC99_SOURCE #define __EXTENSIONS__ #include #
我在使用 File.realpath() 时遇到问题,留下的字符串似乎没有被垃圾收集。在我看来,这像是内存泄漏,但我无法想象这样的事情对于核心库方法来说真的是真的。 考虑以下代码: def strin
我已经养成了使用以下命令在我的编辑器中快速打开相关文件进行代码审查的习惯。 git diff --name-only master... | xargs realpath | someEditor 但
我的一个扩展要求我将 realpath_cache_size 设置为至少 512k。那么我是否必须将它设置在 .htaccess 中,例如: realpath_cache_size = 512k 以及
我已经读过有关 realpath() 的内容,但是是否有一个函数可以传递基本目录和文件名,可以在不解析符号链接(symbolic link)或检查文件是否实际存在的情况下给出以下结果?或者我是否必须使
我已经阅读过有关 realpath() 的内容,但是是否有一个函数可以传递一个基本目录和一个文件名,从而在不解析符号链接(symbolic link)或检查文件是否实际存在的情况下给出以下结果?还是我
我想获取当前执行脚本的路径,我用过 os.path.realpath(__file__),但是,它会返回类似 D:\My Stuff\Python\my_script.py 的字符串,而没有适当的反斜
显然,realpath 有很多问题。在 PHP 5.3.1 中,它会导致随机崩溃。在 5.3.0 及更低版本中,realpath 随机失败并返回 false(当然是针对相同的字符串),而且它总是在 r
我有以下代码: os.chdir(os.path.dirname(os.path.realpath(__file__)) + "/../test") path.append(os.getcwd())
我是一名优秀的程序员,十分优秀!