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性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...