android 文件的读写--调用android自带的文件管理器选择文本文件读写从操作

朔北冥 2017-03-25 03:04:34
这个天尝试着调用android自带的文件文件管理器读取文本文件----其中碰到了些问题--特来求助!!!!

其中调用文件管理器获取文件路径的代码:

private void chooseFile() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(Intent.createChooser(intent, "选择文件"), FILE_SELECT_CODE);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(this, "没有文件管理器,请安装", Toast.LENGTH_SHORT).show();
}
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (resultCode != Activity.RESULT_OK) {
Log.e(TAG, "onActivityResult() error, resultCode: " + resultCode);
super.onActivityResult(requestCode, resultCode, data);
return;
}
if (requestCode == FILE_SELECT_CODE) {
Uri uri = data.getData();
openFile=uri.getPath();
Log.i(TAG,"-------->"+uri.toString());
Log.i(TAG, "------->" + uri.getPath());
originalContent = FileOperation.read(openFile);
originalText.setText(originalContent);
}
super.onActivityResult(requestCode, resultCode, data);
}


读取文本代码:

public static String read(String fileName){
String path = fileName;
String content = "";

File file = new File(path);
if (file.isDirectory())
{
Log.d("TestFile", "The File doesn't not exist.");
}
else
{
try {
InputStream instream = new FileInputStream(file);
if (instream != null)
{
InputStreamReader inputreader = new InputStreamReader(instream);
BufferedReader buffreader = new BufferedReader(inputreader);
String line;
while (( line = buffreader.readLine()) != null) {
content += line + "\n";
}
instream.close();
}
}
catch (FileNotFoundException e)
{
Log.d("TestFile", "The File doesn't not exist.");
}
catch (IOException e)
{
Log.d("TestFile", e.getMessage());
}
}
return content;
}



输出返回的URL.getPaht();url.toString() 发现路径皆不正常不知道哪里错了

...全文
379 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
朔北冥 2017-04-10
  • 打赏
  • 举报
回复
顶帖。。。。。
朔北冥 2017-03-25
  • 打赏
  • 举报
回复
顶帖。。。。求助

80,472

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧