首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将file://方案转换为content://方案

将file://方案转换为content://方案
EN

Stack Overflow用户
提问于 2011-06-10 09:56:10
回答 3查看 11.9K关注 0票数 6

我遇到了使用Droid X的Files应用程序和Astro文件管理器来选择图像文件的问题。这两个应用程序返回方案为"file://“”的选定图像,而Gallery返回方案为"content://“的图像。如何将第一个模式转换为第二个模式。或者我如何解码第二种格式的图像?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-06-10 10:07:01

您可能希望将content://转换为file://

对于图库图像,尝试如下所示:

代码语言:javascript
复制
Uri myFileUri;
Cursor cursor = context.getContentResolver().query(uri,new String[]{android.provider.MediaStore.Images.ImageColumns.DATA}, null, null, null);
if(cursor.moveToFirst())
{
    myFileUri = Uri.parse(cursor.getString(0)).getPath();
}
cursor.close
票数 4
EN

Stack Overflow用户

发布于 2013-06-04 19:07:56

这里的问题是,对于所有的文件,我们不能有一个内容Uri ( content ://)。因为内容uri用于作为MediaStore的一部分的那些文件。例如:图像、音频和视频。

但是,对于支持的文件,我们可以找到它的绝对路径。如下所示的图片-

代码语言:javascript
复制
File myimageFile = new File(path);
Uri content_uri=getImageContentUri(this,myimageFile);

泛型方法如下所示。

代码语言:javascript
复制
public static Uri getImageContentUri(Context context, File imageFile) {
String filePath = imageFile.getAbsolutePath();
Cursor cursor = context.getContentResolver().query(
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
        new String[] { MediaStore.Images.Media._ID },
        MediaStore.Images.Media.DATA + "=? ",
        new String[] { filePath }, null);

if (cursor != null && cursor.moveToFirst()) {
    int id = cursor.getInt(cursor
            .getColumnIndex(MediaStore.MediaColumns._ID));
    Uri baseUri = Uri.parse("content://media/external/images/media");
    return Uri.withAppendedPath(baseUri, "" + id);
} else {
    if (imageFile.exists()) {
        ContentValues values = new ContentValues();
        values.put(MediaStore.Images.Media.DATA, filePath);
        return context.getContentResolver().insert(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    } else {
        return null;
    }
}}
票数 4
EN

Stack Overflow用户

发布于 2011-06-10 10:04:57

使用ContentResolver.openInputStream()或相关方法访问字节流。一般情况下,您不必担心它是file:还是content: URI。

http://developer.android.com/reference/android/content/ContentResolver.html#openInputStream(android.net.Uri

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6301215

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档