android – 从文件URI获取内容URI?

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

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

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

很抱歉缺少源代码,我远离拥有源代码的计算机,但我希望没有它的问题是有道理的,从完整的路径获取内容样式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

解决方法

我弄清楚了.它是这里找到的代码的组合: 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;
    }
}

也许这将有助于未来的人.

相关文章

这篇“android轻量级无侵入式管理数据库自动升级组件怎么实现...
今天小编给大家分享一下Android实现自定义圆形进度条的常用方...
这篇文章主要讲解了“Android如何解决字符对齐问题”,文中的...
这篇文章主要介绍“Android岛屿数量算法怎么使用”的相关知识...
本篇内容主要讲解“Android如何开发MQTT协议的模型及通信”,...
本文小编为大家详细介绍“Android数据压缩的方法是什么”,内...