我只是想把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
以及它改变的基础是什么?
注意:我确实尝试使用Uri
和data.putExtra()
直接发送data.getParcelableExtra()
。它们给出了相同的结果,就好像我发送了字符串并从中构造了一个Uri。
更新:我正在使用File.createTempFile()
,最后添加一个随机数。即使它FileProvider正在生成自己的引用,它仍然不应该触及他的原始文件名,这是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个字符...... !!!这让我觉得它肯定与长度有关。
奇怪的是导致问题的长度。
将文件名保持在16个字符以下可以解决问题。是的,就像那样,不需要任何改变。请注意,如果您使用的是File.createTempFile()
,它将添加5个或更多字符(在我的情况下为9),如here所述。
虽然现在解决了这个问题,但这又引出了另一个问题:如果我想要16个以上的字符怎么办?
如果你想将Uri从一个活动传递到另一个活动,那么请使用这样的意图:
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();