如何在Android中读取文本文件?
我将一个文本文件复制到Android设备上。在我的应用程序中,我想浏览文件,读取并处理它。问题是,我无法找到一种方法,可以可靠地获取文件路径来读取文件 我使用以下代码启动选择器:如何在Android中读取文本文件?,android,Android,我将一个文本文件复制到Android设备上。在我的应用程序中,我想浏览文件,读取并处理它。问题是,我无法找到一种方法,可以可靠地获取文件路径来读取文件 我使用以下代码启动选择器: Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("*/*"); intent.addCategory(Intent.CATEGORY_OPENABLE); try { startActivityForResult(In
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(Intent.createChooser(intent, "Select a File to Upload"), FILE_SELECT_CODE);
} catch (android.content.ActivityNotFoundException ex) {
}
在ActivityResult上,我尝试获取路径,并在不同的设备上观察到不同的行为
安卓8设备:
Uri uri = data.getData();
String path = uri.getPath(); // --> /external_files/info.txt
String fileName = uri.getLastPathSegment(); // --> info.txt
String absPath = Environment.getExternalStorageDirectory().getAbsolutePath() --> /storage/emulated/0
// /storage/emulated/0/info.txt --> I can open the file with this path
Uri uri = data.getData();
String path = uri.getPath(); // --> /document/primary:info.txt
String fileName = uri.getLastPathSegment(); // --> primary:info.txt
String absPath = Environment.getExternalStorageDirectory().getAbsolutePath() --> /storage/emulated/0
// /storage/emulated/0/info.txt --> I can open the file with this path
安卓7设备:
Uri uri = data.getData();
String path = uri.getPath(); // --> /external_files/info.txt
String fileName = uri.getLastPathSegment(); // --> info.txt
String absPath = Environment.getExternalStorageDirectory().getAbsolutePath() --> /storage/emulated/0
// /storage/emulated/0/info.txt --> I can open the file with this path
Uri uri = data.getData();
String path = uri.getPath(); // --> /document/primary:info.txt
String fileName = uri.getLastPathSegment(); // --> primary:info.txt
String absPath = Environment.getExternalStorageDirectory().getAbsolutePath() --> /storage/emulated/0
// /storage/emulated/0/info.txt --> I can open the file with this path
是否有任何机制可以获取读取文件的路径
谢谢,
GL您可以使用此代码获取文件内容,而不是获取路径并再次读取:
Uri uri = data.getData();
InputStreamReader inputStreamReader = new InputStreamReader(getContentResolver().openInputStream(uri));
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder sb = new StringBuilder();
String s;
while ((s = bufferedReader.readLine()) != null) {
sb.append(s);
}
String fileContent = sb.toString();
请注意,这些行需要包装到try-catch块中,并且不要忘记关闭流。您可以从这个代码片段中读取文件
/**
* Helper class. Allowing reading data from a file.
*/
public final class FileDataReader {
/**
* Read a file line by line. Every line from the file will be a new data emission.
*
* @param filename the file being read
*/
public static Observable<String> readFileByLine(@NonNull final String filename) {
return Observable.create(new Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> lineSubscriber) {
InputStream inputStream = FileDataReader.class.getResourceAsStream(filename);
if (inputStream == null) {
lineSubscriber.onError(new IllegalArgumentException(
"File not found on classpath: " + filename));
return;
}
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream,
Charset.forName("UTF-8")));
String line;
try {
while ((line = reader.readLine()) != null) {
lineSubscriber.onNext(line);
}
reader.close();
// no more lines to read, we can complete our subscriber.
lineSubscriber.onCompleted();
} catch (IOException ex) {
lineSubscriber.onError(ex);
}
}
});
}
}
/**
*助手类。允许从文件中读取数据。
*/
公共最终类FileDataReader{
/**
*逐行读取文件。文件中的每一行都将是一个新的数据发射。
*
*@param filename正在读取的文件
*/
公共静态可观察readFileByLine(@NonNull final String filename){
返回Observable.create(newobservable.OnSubscribe(){
@凌驾
公共无效呼叫(订户在Kotlin,您应该能够执行以下操作:
val inputStream = context.contentResolver.openInputStream(uri)
val fileContent = inputStream.bufferedReader().use {it.readText()}
您不需要路径。调用getContentResolver().openInputStream(uri)
获取InputStream
,并从中读取它。