簡體   English   中英

從文件URI獲取內容URI?

[英]Get a Content URI from a File 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

我弄清楚了。 它是這里找到的代碼的組合: 轉換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;
    }
}

也許這將有助於未來的人。

所以我的問題是,如果有人知道如何將完整的文件路徑/ URI(文件://)轉換為內容樣式URI(內容://)?

實現ContentProvider FileProvider提供了一個開箱即用的解決方案,用於提供本地文件。

我不確定你用來設置壁紙的技術,但最簡單的方法可能是使用不需要任何URI的WallpaperManager.setStream()

另請注意,如果文件可公開訪問,則文件URI僅在應用程序之間起作用,因此內容URI是更通用的解決方案。

使用內容URI意味着ContentProvider將為該文件提供服務。 哪一個取決於您的文件所在的位置。

如果您的應用程序具有對文件的直接讀取權限,則可以使用支持庫的FileProvider類在應用程序中實現內容提供程序,但實際上只有在文件位於專用數據存儲中時才能使用你的應用程序。

如果將圖像添加到系統媒體庫,則應該使用MediaStore提供的URI。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM