使用ACTION_OPEN_DOCUMENT打开文本文件 [英] Open text file using ACTION_OPEN_DOCUMENT
问题描述
我想设置一个的EditText一个简单的txt文件的内容。看开发者页面后,我遇到了一些code,获取照片绊倒了。我编辑什么,我认为会适合我的需要,但它不工作:
I'm trying to set an 'EditText' to the contents of a simple txt file. After looking at the developer page I stumbled across some code that gets a photo. I edited to what I thought would suit my needs but it doesn't work:
private void importText(){
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.setType("text/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0 && resultCode == RESULT_OK) {
String textContent = data.getDataString();
mEditText.setText(textContent);
}
}
这样做是把它的似乎是在的EditText
不是它的内容的文件位置的字符串名称。我的应用程序目前只支持19 API,所以我想我将能够使用这个作为一个功能。这个函数可以使用 ACTION_OPEN_DOCUMENT
或者我需要做点别的?
What this does is it puts the string name of what appears to be the files location in the EditText
not the contents of it. My app right now only supports API 19 so I thought I would be able to use this as a feature. Is this function possible using ACTION_OPEN_DOCUMENT
or do I need to do something else?
推荐答案
您需要检索从URI的输入流作为的文档。 (见获取输入流)。
You need to retrieve an Input Stream from the Uri as explained in the documentation. (Section "Get an Input Stream").
这篇关于使用ACTION_OPEN_DOCUMENT打开文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!