获取一个内容URI从文件URI? [英] Get a Content URI from a File URI?

查看:611
本文介绍了获取一个内容URI从文件URI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是下载管理器下载一个图像系统的画廊,然后在广播接收器使用一个Intent设置图像作为壁纸(一旦下载成功)。

一切都工作正常,但最近对4.4我开始在照片/谷歌+应用程序异常,因为它是期待一个内容URI,而不是一个文件URI。

所以我的问题是,如果有人知道如何将一个完整的文件路径/ URI(文件://)到内容风格的URI(内容://)?

对不起,缺乏源头code,我远离了源计算机,但我希望这个问题是有道理的,没有它,从一个完整的路径获得的内容风格的URI。

编辑:

图片被复制到系统的画廊或媒体库,而不是救了我的应用程序内部的StoreAge的。

下面是什么,我想转换一个例子:

 文件:///storage/emulated/0/Pictures/Rockstar/image.jpg
 

 内容:// /内部/图片/媒体/ 445
 

编辑2:

下面是我从Google+应用程序时出现错误:

 十月4号至21日:50:35.090:E / AndroidRuntime(7220):致命异常:主要
10月4日至21日:50:35.090:E / AndroidRuntime(7220):工艺:com.google.android.apps.plus,PID:7220
10月4日至21日:50:35.090:E / AndroidRuntime(7220):java.lang.RuntimeException的:无法恢复活动
{com.google.android.apps.plus/com.google.android.apps.photos.phone.SetWallpaperActivity}:
java.lang.IllegalArgumentException:如果图像URI必须是内容计划类型
 

下面是我用来让用户设置壁纸的code:

 字符串uriString中= c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
乌里U = Uri.parse(uriString中);
意图wall_intent =新的意图(Intent.ACTION_ATTACH_DATA);
wall_intent.setDataAndType(U,图像/ *);
wall_intent.putExtra(MIMETYPE,图像/ *);
意图chooserIntent = Intent.createChooser(wall_intent,
    设置);
chooserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
尝试 {
    context.startActivity(chooserIntent);
}
 

其中, uriString中是:

 文件:///storage/emulated/0/Pictures/Rockstar/image.jpg
 

解决方案

我能弄明白。这是在code组合在这里找到:<一href="http://stackoverflow.com/questions/14754358/converting-android-image-uri/14832617#14832617">Converting Android的图像URI 并扫描媒体文件下载后。

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

 字符串uriString中= c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));

//更新系统
乌里U = Uri.parse(uriString中);
context.sendBroadcast(新意图(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,U));

//使用文件获取绝对路径,这是非常重要的
文件wallpaper_file =新的文件(u.getPath());
乌里contentURI = getImageContentUri(上下文,wallpaper_file.getAbsolutePath());
 

由于某种原因开始介质扫描仪,newing文件,以及获取绝对路径是很重要的,我不知道是什么原因,但我不能花更多的时间在这!

的方式从一个文件URI转换为URI如下(摘自链接StackOver流后内容:

 公共静态乌里getImageContentUri(上下文的背景下,字符串ABSPATH){
    Log.v(TAG,getImageContentUri:+ ABSPATH);

    光标光标= context.getContentResolver()查询(
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI
        ,新的String [] {} MediaStore.Images.Media._ID
        ,MediaStore.Images.Media.DATA +=?
        ,新的String [] {} ABSPATH,NULL);

    如果(光标=空&安培;!&安培; cursor.moveToFirst()){
        INT的id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
        返回Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,Integer.toString(ID));

    }否则如果(!absPath.isEmpty()){
         ContentValues​​值=新ContentValues​​();
         values​​.put(MediaStore.Images.Media.DATA,ABSPATH);
         返回context.getContentResolver()。插入(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,价值观);
    } 其他 {
        返回null;
    }
}
 

也许这将帮助别人的未来。

I am using the DownloadManager to download an image to the system's gallery and then in the Broadcast receiver (once the download succeeds) using an Intent to set the image as the wallpaper.

Everything was working fine but then recently on 4.4 I started to get an exception in the Photos/Google+ app because it is expecting a content URI and not a file URI.

So my question is if anyone knows how to convert a full file path/URI (file://) into a content style URI (content://)?

Sorry for the lack of source code, I am away from the computer that has the source, but I hope the question makes sense without it, get a content style uri from a full path.

EDIT:

The image is copied into the system's gallery or media gallery, not saved within my apps internal storeage.

Here is an example of what I want to convert:

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

to

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

EDIT 2:

Here is the error that I get from the Google+ app:

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

Here is the code that I use to let the user set the wallpaper:

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);
} 

Where uriString is:

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

解决方案

I was able to figure it out. It was a combination of the code found here: Converting android image URI and scanning the media file after downloading.

So after the file finished downloading I get the path and do the following:

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());

For some reason starting the media scanner, newing the file, and getting the absolute path are important, I'm not exactly sure why but I can't spend any more time on this!

The way to convert from a file URI to a content URI is as follows (taken from the linked StackOver flow post:

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;
    }
}

Maybe this will help someone in the future.

这篇关于获取一个内容URI从文件URI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆