【发布时间】:2017-01-24 21:01:59
【问题描述】:
我知道我的问题已被多次解决,但我找不到任何可以帮助我解决具体问题的方法。
我打算从系统的任何地方选择一个文件:
private void showFileChooser() {
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) {
// Potentially direct the user to the Market with a Dialog
Toast.makeText(this, "Please install a File Manager.",
Toast.LENGTH_SHORT).show();
}
}
我在这里接受了这个意图:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case FILE_SELECT_CODE:
if (resultCode == RESULT_OK) {
// Get the Uri of the selected file
Uri uri = data.getData();
}
我有这样的上传方法:
private void upload(File file){
RequestParams params = new RequestParams();
try {
params.put("fileToUpload", file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
AsyncHttpClient client = new AsyncHttpClient();
client.post("http://...", params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
System.out.println("statusCode "+statusCode);//statusCode 200
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
}
});
}
问题是我不知道如何将 onActivity 和 upload 这两个方法“结合”起来,因为我不知道如何处理从 Intent 中获得的信息,以便 AsyncHttpClient 可以使用它。
我尝试将 Uri 转换为绝对路径,但无法管理(似乎适用于图像的在线解决方案)。我也无法将 uri “转换”为文件。
有什么办法可以做到吗?我错过了什么吗?
【问题讨论】:
标签: java android android-intent android-async-http