从文件URI获取内容URI?

时间:2014-04-21 23:15:02

标签: android

我正在使用DownloadManager将图像下载到系统的图库,然后在广播接收器中(下载成功后)使用Intent将图像设置为壁纸。

一切都运行良好,但最近在4.4我开始在Photos / Google +应用程序中获得例外,因为它期望内容URI而不是文件URI。

所以我的问题是,是否有人知道如何将完整文件路径/ URI(file://)转换为内容样式URI(content://)?

很抱歉缺少源代码,我远离拥有源代码的计算机,但我希望没有它的问题是有道理的,从完整的路径获取内容样式uri。

修改

图像被复制到系统的图库或媒体库中,而不是保存在我的应用内部存储中。

以下是我要转换的示例:

file:///storage/emulated/0/Pictures/Rockstar/image.jpg

content://media/internal/images/media/445

编辑2:

以下是我从Google+应用中收到的错误:

04-21 10:50:35.090: E/AndroidRuntime(7220): FATAL EXCEPTION: main
04-21 10:50:35.090: E/AndroidRuntime(7220): Process: com.google.android.apps.plus, PID: 7220
04-21 10:50:35.090: E/AndroidRuntime(7220): java.lang.RuntimeException: Unable to resume activity
{com.google.android.apps.plus/com.google.android.apps.photos.phone.SetWallpaperActivity}:  
java.lang.IllegalArgumentException: Image URI must be of the content scheme type

以下是我用来让用户设置壁纸的代码:

String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
Uri u = Uri.parse(uriString);
Intent wall_intent =  new Intent(Intent.ACTION_ATTACH_DATA);
wall_intent.setDataAndType(u, "image/*");
wall_intent.putExtra("mimeType", "image/*");
Intent chooserIntent = Intent.createChooser(wall_intent,
    "Set As");
chooserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);      
try {
    context.startActivity(chooserIntent);
} 

uriString的位置:

file:///storage/emulated/0/Pictures/Rockstar/image.jpg

3 个答案:

答案 0 :(得分:7)

我能够弄清楚。它是此处的代码组合:Converting android image URI并在下载后扫描媒体文件。

所以在文件完成下载后,我得到了路径并执行以下操作:

String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));

//Update the System
Uri u = Uri.parse(uriString);                       
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, u));

//Get the abs path using a file, this is important          
File wallpaper_file = new File(u.getPath());
Uri contentURI = getImageContentUri(context, wallpaper_file.getAbsolutePath());

出于某种原因,启动媒体扫描仪,新建文件,获取绝对路径非常重要,我不确定为什么,但我不能再花时间在这上面了!

从文件URI转换为内容URI的方法如下(取自链接的StackOver流程帖子:

public static Uri getImageContentUri(Context context, String absPath) {
    Log.v(TAG, "getImageContentUri: " + absPath);

    Cursor cursor = context.getContentResolver().query(
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI
        , new String[] { MediaStore.Images.Media._ID }
        , MediaStore.Images.Media.DATA + "=? "
        , new String[] { absPath }, null);

    if (cursor != null && cursor.moveToFirst()) {
        int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
        return Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI , Integer.toString(id));

    } else if (!absPath.isEmpty()) {
         ContentValues values = new ContentValues();
         values.put(MediaStore.Images.Media.DATA, absPath);
         return context.getContentResolver().insert(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    } else {
        return null;
    }
}

也许这将有助于将来。

答案 1 :(得分:1)

  

所以我的问题是,是否有人知道如何将完整文件路径/ URI(file://)转换为内容样式URI(content://)?

实施ContentProviderFileProvider为提供本地文件提供了开箱即用的解决方案。

答案 2 :(得分:1)

我不确定您使用的技术来设置壁纸,但最简单的方法可能是使用不需要任何URI的WallpaperManager.setStream()

另请注意,如果文件可公开访问,则文件URI仅适用于应用之间,因此内容URI是更常用的解决方案。

使用内容URI意味着ContentProvider将为该文件提供服务。哪一个取决于您的文件所在的位置。

如果您的应用具有对该文件的直接读取权限,则可以使用例如支持库的FileProvider类在您的应用中实现内容提供商,但这应该仅在文件中使用位于应用的私人数据存储中。

如果图像已添加到系统媒体库,您应该使用MediaStore提供的URI。

相关问题