从文件URI获取内容URI?

塞尔辛

我正在使用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
塞尔辛

我能够弄清楚。它是在这里找到的代码的组合:转换android图像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;
    }
}

也许这会在将来对某人有所帮助。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章