Android:Uri文件名混淆

时间:2017-12-26 04:41:52

标签: android uri

我只是想把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以及它改变了什么基础?

注意:我确实尝试使用Uridata.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个字符...... !!!这让我觉得它肯定与长度有关。

2 个答案:

答案 0 :(得分:0)

奇怪的是导致问题的长度。

将文件名保持在16个字符以下可以解决问题。是的,就像那样,不需要任何改变。请注意,如果您使用File.createTempFile(),则会添加5个或更多字符(在我的情况下为9),如上所述here

虽然现在解决了这个问题,但这又引出了另一个问题:如果我想要16个以上的字符怎么办?

答案 1 :(得分:-3)

如果你想将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();
相关问题