简体   繁体   English

有没有办法从手机的SD卡输入文本文件?

[英]Is there a way to input a text file from phone's sdcard?

I'm making a Phonegap app that requires a text file input from the user. 我正在制作一个Phonegap应用程序,该应用程序需要用户输入文本文件。 I've tried using <input type="file"> but it only allows me to choose a file from the gallery or a music file. 我尝试使用<input type="file">但它只允许我从图库中选择文件或音乐文件。

Is there any way for the user to browse the phone's sdcard using the native file browsers for a text file on both iOS and Android? 用户是否可以使用本机文件浏览器在iOS和Android上使用文本文件浏览器浏览手机的SD卡? Or is making my own file browser the only option available? 还是使自己的文件浏览器成为唯一可用选项? Is there no simple way to let the user plug in a file to the app? 没有简单的方法让用户将文件插入应用程序吗?

To put it simply: No 简而言之:否

iOS don't have an accessible file system not even for developers. iOS甚至没有针对开发人员的可访问文件系统。 Also none of the iOS devices support SD cards. 此外,所有iOS设备都不支持SD卡。

In android you can more do it. 在android中,您可以做更多。

To make your app compatible with standard text files by registering it into it's manifest file. 通过将其注册到清单文件中来使其与标准文本文件兼容。 Take a look at this example from google. 看一下Google的这个例子

To access the files in the SD card, if for instance you want to scan for a certain file type, take a look at this guide . 要访问SD卡中的文件,例如,如果要扫描某种文件类型,请参阅本指南

//Find the directory for the SD Card using the API // Don't hardcode "/sdcard" //使用API​​查找SD卡的目录// 不要硬编码“ / sdcard”

File sdcard = Environment.getExternalStorageDirectory();

//Get the text file //获取文本文件

File file = new File(sdcard,"file.txt");

//Read text from file //从文件中读取文本

StringBuilder text = new StringBuilder();

try {
    BufferedReader br = new BufferedReader(new FileReader(file));
    String line;

    while ((line = br.readLine()) != null) {
        text.append(line);
        text.append('\n');
    }
}
catch (IOException e) {
    //You'll need to add proper error handling here
}

//Find the view by its id //通过ID查找视图

TextView tv = (TextView)findViewById(R.id.text_view);

//Set the text //设置文字

tv.setText(text);

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

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