Android: Uri 文件名混淆
Android: Uri filename confusion
我只是想将 uri 从一个 activity 传递到另一个。在这样做的同时,我注意到我收到了一个没有找到文件的异常,我可以通过打印出来看到结束文件名不一样。 (只是 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 个字符...!!!这让我觉得肯定和长度有关系。
如果你想将一个 activity 的 Uri 传递给另一个 activity,那么请使用这样的意图:
Uri Uri1=new Uri();
class A:
Intent intent=new Intent(A.this,B.class)
intent.set Data(Uri1);
start Activity(intent);
Class B
Intent intent=getIntent();
Uri uri=intent.getData();
奇怪的是导致问题的长度。
将文件名保持在 16 个字符以内可以解决问题。是的,就像那样工作,不需要任何更改。请注意,如果您使用 File.createTempFile()
,它将添加 5 个或更多字符 (在我的例子中是 9) 如前所述 here.
虽然这暂时解决了问题,但这会引出另一个问题:如果我想要 16 个以上的字符怎么办?
我只是想将 uri 从一个 activity 传递到另一个。在这样做的同时,我注意到我收到了一个没有找到文件的异常,我可以通过打印出来看到结束文件名不一样。 (只是 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 个字符...!!!这让我觉得肯定和长度有关系。
如果你想将一个 activity 的 Uri 传递给另一个 activity,那么请使用这样的意图:
Uri Uri1=new Uri();
class A:
Intent intent=new Intent(A.this,B.class)
intent.set Data(Uri1);
start Activity(intent);
Class B
Intent intent=getIntent();
Uri uri=intent.getData();
奇怪的是导致问题的长度。
将文件名保持在 16 个字符以内可以解决问题。是的,就像那样工作,不需要任何更改。请注意,如果您使用 File.createTempFile()
,它将添加 5 个或更多字符 (在我的例子中是 9) 如前所述 here.
虽然这暂时解决了问题,但这会引出另一个问题:如果我想要 16 个以上的字符怎么办?