简体   繁体   English

在android中链接和复制外部(可移动存储)文件

[英]Linking and copying an external (Removable storage) file in android

I have an open question I would like to ask before starting development of functionality, only to learn more about the subject afterwards, just to start all over again ;) I tried searching, but am not really sure what to search for. 在开始开发功能之前,我有一个开放的问题要问,只是以后要学习更多有关该主题的知识,只是从头开始;)我尝试搜索,但不确定要搜索什么。 Therefor, I would like to pick your brains a bit here... 为此,我想在这里稍加注意...

Here is the current situation: I have an app that stores activity/event logs. 当前情况如下:我有一个存储活动/事件日志的应用程序。 In a simplified example: "Person A filled a glass with 100ml of water" 在一个简化的示例中:“人员A用100毫升水填充了玻璃杯”

This is stored as a record in a single table. 这作为记录存储在单个表中。

Now, for the requirement of the new functionality: I connect an external storage device via USB to my tablet. 现在,对于新功能的要求:我通过USB将外部存储设备连接到平板电脑。 On this storage device there are several text files. 在此存储设备上,有几个文本文件。 Each file contains parameters of an activity/event, similar to the one described above, but with different data. 每个文件都包含活动/事件的参数,与上述参数类似,但数据不同。

I would like to create a function to import these files, read some of their content into the database as new records, and then store the file somewhere on the tablet, with a link to the file as a field in the DB. 我想创建一个函数来导入这些文件,将它们的某些内容作为新记录读入数据库,然后将该文件存储在平板电脑上的某个位置,并带有指向该文件的链接作为DB中的字段。 Ideally I would have liked to store the contents of the entire text file in a field, or a field in a related 1:1 table, but these files could be as big as 1MB (1MB would be very rare, but possible. Avg is around 100KB). 理想情况下,我希望将整个文本文件的内容存储在一个字段中,或者存储在相关的1:1表中的一个字段中,但是这些文件可能大到1MB(1MB很少,但是可能。大约100KB)。

So, my questions are: 因此,我的问题是:

  • Is it at all possible to copy a file from an external storage device, linked via USB, to the android device's SD card/internal memory? 是否可以将通过USB链接的外部存储设备中的文件复制到android设备的SD卡/内部存储器中? (I can access this storage device using a file explorer app on my tablet) (我可以使用平板电脑上的文件浏览器应用程序访问此存储设备)
  • If so, what is the best way to go about this? 如果是这样,最好的方法是什么?
  • If not, what are my alternatives? 如果没有,我有什么选择?
  • Also, if not possible to copy and locally store the files, is it still possible to access their contents on the external storage? 另外,如果无法复制和本地存储文件,是否仍然可以在外部存储器上访问其内容? In which case I will then just import the data I need, and not store a copy of the original file. 在这种情况下,我将仅导入所需的数据,而不存储原始文件的副本。

Thanks in advance, all replies are much appreciated! 在此先感谢您,感谢所有回复!

No one wants to take the chance? 没有人想借此机会吗? :) :)

For future readers with the same problem: It is possible to read a file from a usb connected device. 对于将来遇到相同问题的读者:可以从USB连接的设备读取文件。 Keep in mind that reading the file directly from the file system is different than reading it as an attachment in a mail for instance. 请记住,直接从文件系统读取文件不同于将其作为邮件附件读取。 Not much more than a different path though ;) 只是一条不同的路径而已;)

I ended up reading the file into a DB, splitting the lines across a record. 我最终将文件读取到数据库中,将行拆分成一条记录。 In other words, not adding the whole file (or even a complete line) in a field, but rather parsing the file into a single record, split up into various fields. 换句话说,不是在字段中添加整个文件(甚至是整行),而是将文件解析为单个记录,然后分成多个字段。

Two things I came across that might help you. 我遇到的两件事可能会对您有所帮助。 Reading a file is pretty fast, but do not make the mistake I made by writing these lines to the database one by one. 读取文件的速度非常快,但不要犯我逐行将这些行写入数据库所犯的错误。 Better to add them all to a transaction, and commit at once. 最好将它们全部添加到事务中,然后立即提交。 The other, you can use a read buffer to read the file, instead of the default inputstream. 另外,您可以使用读取缓冲区来读取文件,而不是默认的输入流。 This also speeds up reading of large files. 这也可以加快大型文件的读取速度。

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

相关问题 在Android中检测可移动外部存储 - Detect removable external storage in Android Android从可移动SD卡删除文件(第二个外部存储) - Android delete file from removable sd card (second external storage) 将资源文件复制到Android上的外部存储不起作用 - Copying resource file to external storage on android not working 如何在 android 中访问外部存储(可移动 SD 卡)? - How to get access to External Storage (The removable SD Card) in android? 将文件保存到SD卡上的“特定”文件夹中(可移动外部存储) - Save file to Specific folder on SD card (External Storage Removable) 如何从可移动SD卡(第二外部存储)中删除文件? - How to delete file from removable sd card (second external storage)? Android:如何在外部(可移动)SD卡中创建文件夹和文件? - Android: How to create folder and file in external(removable) SD card? 使用外部Android应用程序安全打开文件,而无需将文件复制到外部存储器 - Open file securely with an external Android application without copying the file onto the external storage 如何在android中找到内部(默认手机存储)和外部存储(可移动SD卡)? - How to find Internal(Default Phone Storage ) and External Storage(Removable sd card) in android? 将文件从原始存储复制到外部存储 - Copying file from raw to external storage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM