UIImage(contentsOfFile:) returns nil for a plain filesystem path ("No content provider")

オープン 初心者向け
#577 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 5 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
74/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
android, swift
領域
mobile

調査の方向性

UIImage.swift の142行目から始めます。ここでは contentsOfFile がパスを Uri.parse に渡しています。UIImage.kt の decodeBitmapFromUri 周辺にある URI のデコード処理を読み、どのように開かれるかを確認します。Android で、提供された url.path の例を使って再現し、絶対ファイルシステムパスが読み込まれる一方で、content:// と file:// のパスも引き続き機能することを確認します。

索引モデルが issue の本文から書いたものです。

説明

UIImage(contentsOfFile:) passes the path straight to Uri.parse(path) (UIImage.swift, line 142 in 1.63.0). A plain filesystem path such as /data/user/0/<pkg>/files/foo.jpg has no scheme, so ContentResolver rejects it and the initializer returns nil:

W SkipUI : Caused by: java.io.FileNotFoundException: No content provider: /data/user/0/com.example/files/EtoilesDeKal/v1/e01/e01_a68_p03.jpg
W SkipUI :   at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:2031)
W SkipUI :   at android.graphics.ImageDecoder$ContentResolverSource.createImageDecoder(ImageDecoder.java:297)
W SkipUI :   at skip.ui.UIImage$Companion.decodeBitmapFromUri(UIImage.kt:342)

Reduced example (not run in isolation; observed in our app on API 36 emulator and a physical device):

let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("photo.jpg")
// file exists and decodes fine with BitmapFactory.decodeFile
let image = UIImage(contentsOfFile: url.path) // nil on Android, non-nil on iOS

On iOS contentsOfFile takes a filesystem path, so shared code calling it with url.path silently shows nothing on Android.

The local fix we apply (works for us on device):

let uri = path.hasPrefix("/") ? Uri.fromFile(java.io.File(path)) : Uri.parse(path)

which keeps content:// and file:// strings working as before.

主要言語
Swift
スター
332
フォーク
91
平均マージ
4日 9時間
マージ済み PR(30日)
10

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

skiptools/skip-ui のほかの issue

skiptools/skip-ui の issue をすべて見る

似ている issue

Swift の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。