【发布时间】:2018-06-07 00:33:19
【问题描述】:
我只是想将 uri 从一个活动传递到另一个活动。在这样做的同时,我注意到我收到了一个找不到文件的异常,我可以通过打印出来看到最终文件名不一样。 (只是path 字符串和uri.getEncodedPath())
所以我尝试发送字符串并重构 uri,如下所示:
Log.e("debug_path_string", data.getExtras().getString(CONSTANT));
# try { Uri photoUri = Uri.fromFile(new File(data.getExtras().getString(CONSTANT))); }
try { Uri photoUri = Uri.parse(data.getExtras().getString(CONSTANT)); }
catch (Exception e) { Log.e("IO", e.getMessage()); }
Log.d("debug", photoUri.getEncodedPath());
pageListAdapter.append(photoUri);
还有日志
12-26 04:18:04.172 4425-4425/com.example.myawesomeapp E/debug_path_string: /storage/emulated/0/Android/data/com.example.myawesomeapp/files/Pictures/JPEG_20171226_041803672615875.jpg
12-26 04:18:04.172 4425-4425/com.example.myawesomeapp D/debug: /document_images/JPEG_20171226_041746-1723016833.jpg
12-26 04:18:04.225 4425-4425/com.example.myawesomeapp W/Glide: Failed to find GeneratedAppGlideModule. You should include an annotationProcessor compile dependency on com.github.bumptech.glide:compiler in your application and a @GlideModule annotated AppGlideModule implementation or LibraryGlideModules will be silently ignored
12-26 04:18:04.400 4425-4425/com.example.myawesomeapp W/Glide: Load failed for content://com.example.myawesomeapp.fileprovider/document_images/JPEG_20171226_041746-1723016833.jpg
是
file://storage/emulated/0/Android/data/com.example.myawesomeapp/files/Pictures/JPEG_20171226_041803672615875.jpg
content://com.example.myawesomeapp.fileprovider/document_images/JPEG_20171226_041746-1723016833.jpg
# provided
# com.example.myawesomeapp.fileprovider/document_images resolves path
# storage/emulated/0/Android/data/com.example.myawesomeapp/files/Pictures
等价的?
显然加载失败是因为找不到这样的文件。 (我检查了路径,并且存在一个文件名与字符串路径相同的文件)。
有人能解释一下为什么Uri.parse() 或Uri.fromFile(new file(path)) 会尝试创建与原始文件名不同的文件名吗?
为什么Android 似乎不喜欢JPEG_20171226_041803672615875 而喜欢JPEG_20171226_041746-1723016833,它改变的依据是什么?
注意:我确实尝试使用data.putExtra() 和data.getParcelableExtra() 直接发送Uri。他们给出的结果与我发送字符串并从中构造一个 Uri 的结果相同。
更新:我使用的是File.createTempFile(),它在末尾附加了一个随机数。即使 FilesProvider 正在生成自己的引用,它仍然不应该触及原始文件名 JPG_date_time。
在观察它的变化时,我才注意到这个奇怪的东西!
|......16.......||.......rest......|
JPEG_20171226_041803672615875.jpg
JPEG_20171226_041746-1723016833.jpg
# Running one more example
JPEG_20171226_121309-2003514507.jpg
JPEG_20171226_121239-882490989.jpg
|......16.......||.......rest......|
它保留了前 16 个字符...!!!这让我觉得这肯定与长度有关。
【问题讨论】:
-
看看 photoUri.toString()。
-
我很惊讶为什么文档中没有提到这 16 个字符的内容,甚至作为警告