简体   繁体   English

Android从uri打开文件输入流

[英]Android open file input stream from uri

I'm trying to upload a file by copying it from mobile app to server app running on windows.我正在尝试通过将文件从移动应用程序复制到 Windows 上运行的服务器应用程序来上传文件。 I used a file chooser to let the user select the file:我使用文件选择器让用户选择文件:

public void openFile(View view) {
        Intent intent = new Intent();
        intent.setType("*/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select file"), LEARN_TREE);
} 

Then i got the uri from the intent of onActivityResult.然后我从 onActivityResult 的意图中得到了 uri。 First question here is why it displays a file named "servo.dat" as numbers (in this case it shows "5889")?这里的第一个问题是为什么它将名为“servo.dat”的文件显示为数字(在本例中显示为“5889”)?

After that I put the uri as an extra into another intent and use that intent to start another activity.之后,我将 uri 作为额外内容放入另一个意图中,并使用该意图开始另一个活动。 In the second activity I retrieve the uri.在第二个活动中,我检索 uri。 Now I'd like to use FileInputStream to read bytes from my file in order to write them to the ObjectOutputStream created from Socket.getOutputStream().现在我想使用 FileInputStream 从我的文件中读取字节,以便将它们写入从 Socket.getOutputStream() 创建的 ObjectOutputStream。 Here is where it doesn't work.这是它不起作用的地方。 Basically the path provided here基本上这里提供的路径

FileInputStream fis = new FileInputStream(uri.getPath());

is incorrect.是不正确的。 If I check on my device the file location is Download/servo.dat, the Uri in the app shows Download/5889 and the absolute path that I tried retrieving using a UriUtils library found online shows storage/emulated/0/Download/servo.dat but this one doesn't actually exist on my phone.如果我在我的设备上检查文件位置是 Download/servo.dat,应用程序中的 Uri 显示 Download/5889,而我尝试使用在线找到的 UriUtils 库检索的绝对路径显示 storage/emulated/0/Download/servo。 dat 但这个在我的手机上实际上并不存在。

I think it's not so hard but I'm getting confused since I'm new to both Android app development and Android itself, please help!我认为这并不难,但我很困惑,因为我是 Android 应用程序开发和 Android 本身的新手,请帮忙!

I'm open to any good solution, I saw online there is the ContentResolver class that should be helpful but I didn't manage to understand how to use it :|我对任何好的解决方案持开放态度,我在网上看到应该有帮助的 ContentResolver 类,但我无法理解如何使用它:|

First question here is why it displays a file named "servo.dat" as numbers (in this case it shows "5889")?这里的第一个问题是为什么它将名为“servo.dat”的文件显示为数字(在本例中显示为“5889”)?

Because it is not a file.因为它不是一个文件。 It is a piece of content, and you are attempting to treat the path portion of a Uri as a filesystem path, which is is not.它是一段内容,您试图将Uri的路径部分视为文件系统路径,但Uri并非如此。

If you want a display name for the content:如果您想要内容的显示名称:

  • Call DocumentFile.fromSingleUri() , passing in your Uri , to create a DocumentFile调用DocumentFile.fromSingleUri() ,传入您的Uri ,以创建一个DocumentFile
  • Call getDisplayName on the DocumentFileDocumentFile上调用getDisplayName

Basically the path provided here is incorrect基本上这里提供的路径不正确

That is because you are trying to treat the path portion of a Uri as a filesystem path, which it is not.那是因为您试图将Uri的路径部分视为文件系统路径,而事实并非如此。

To get an InputStream , call openInputStream() on a ContentResolver , passing in your Uri .要获取InputStream ,请在ContentResolver上调用openInputStream() ,传入您的Uri See the documentation .请参阅文档 So, for example, from a method in an Activity , you would use InputStream inputStream = getContentResolver().openInputStream(uri);因此,例如,从Activity的方法中,您将使用InputStream inputStream = getContentResolver().openInputStream(uri); . .

I'm getting confused since I'm new to both Android app development and Android itself我很困惑,因为我是 Android 应用程序开发和 Android 本身的新手

You may wish to consider reading a book on Android app development or taking a course in Android app development.您可能希望考虑阅读有关 Android 应用程序开发的书籍或参加 Android 应用程序开发课程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM