gpt4 book ai didi

安卓 : How to get file data from a Filechooser?

转载 作者:行者123 更新时间:2023-11-30 02:56:51 26 4
gpt4 key购买 nike

我有一个文件选择器,我在 WebView 中调用它来上传文件。允许我在文件选择器 Activity 中检索文件的方法如下:

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Option o = adapter.getItem(position);
if (o.isFolder() || o.isParent()) {
currentDir = new File(o.getPath());
fill(currentDir);
} else {
//onFileClick(o);
fileSelected = new File(o.getPath());
Intent intent = new Intent();
intent.putExtra("fileSelected", fileSelected.getAbsolutePath());
setResult(Activity.RESULT_OK, intent);
finish();
}
}

这只允许我在我的 webview 的 onActivityResult 中获取路径(它调用我的文件选择器来上传文件)。onActivityResult 方法由下面的代码给出。

如果我使用手机中安装的另一个应用程序而不是我的文件选择器,我会使用:

Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();

然后结果发送:

 this.mUploadMessage.onReceiveValue(uri);

从那时起,它就正常工作了。但是对于我的文件选择器 intent.getData () 等于 null

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == FILECHOOSER_RESULTCODE) {
if (null == this.mUploadMessage) {
return;
}
if (resultCode != RESULT_OK) {
mUploadMessage.onReceiveValue(null);
var = 0;
return;
}
// Toast.makeText(getApplicationContext() , "onActivityResult()" + String.valueOf(requestCode) ,Toast.LENGTH_LONG).show();
// Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();


// this.mUploadMessage = null;
String fileSelected = intent.getStringExtra("fileSelected");
Bundle result = intent.getExtras();
//result = Uri.parse(fileSelected);

//this.mUploadMessage.onReceiveValue(result);

if (resultCode == RESULT_OK) {
if (intent != null) {
// Get the URI of the selected file
final Uri uri = intent.getData();
Log.i("TOTOTOT", "Uri = " + uri.toString());
Toast.makeText(this, fileSelected + " " + uri.toString() , Toast.LENGTH_SHORT).show();
this.mUploadMessage.onReceiveValue(uri);
}
}
super.onActivityResult(requestCode, resultCode, intent);

}
}

为了检索数据和路径,而不仅仅是发送文件的路径,我应该在我的两个方法中返回什么?应该是所有包还是您知道如何在使用 Intent.getData() 时获取数据?

最佳答案

从 onActivityResult() 中选择的目录获取数据需要什么
你选择了路径,这样你就可以从那里读取你想要的文件..

使用此方法将文件作为字符串读取

private String readURLFromPath(File filePath){
String dataString = "";


// i have kept text.txt in the sd-card
if (file.exists()) // check if file exist
{
// Read text from file
StringBuilder text = new StringBuilder();

try {
BufferedReader br = new BufferedReader(new FileReader(filePath));
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
}
// Set the text
dataString = text.toString();
}
return dataString ;
}

此方法将以字符串形式返回数据,现在您可以将此字符串数据发送到服务器...

关于安卓 : How to get file data from a Filechooser?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23129086/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com