我有一个项目,我需要允许用户上传图像和视频到服务器...目前用户可以选择图像和视频..但我不知道如何获得真正的文件路径然后上传使用AsyncHttp Post ...
这是我在下面选择图片的代码:
pphoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
});
这是我的onactivityresult代码
protected void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent){
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case PICK_IMAGE_REQUEST: // Do your stuff here...
if (resultCode == RESULT_OK) {
final String path = imageReturnedIntent.getData().getPath();
final File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
Uri selectedData = imageReturnedIntent.getData();
final Button up = (Button) findViewById(R.id.up);
final ImageView etPhoto = (ImageView) findViewById(R.id.imgg);
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(
selectedData, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
final String filePath = cursor.getString(columnIndex);
cursor.close();
final Bitmap dImage = BitmapFactory.decodeFile(filePath);
etPhoto.setImageURI(selectedData);
然后,如果我想发布到服务器...这是我正在使用的代码但不工作......
AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.put("token", "token");
try {
params.put("photo", file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
client.post("http://10.205.165.237/Android/uploadimages.php", 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) {
}
});
现在我的问题是我不知道如何获取用于上传的真实文件路径,我不确定AsyncHttp帖子是否正确cos我已经尝试了PHP代码我将文件发送到html表单它工作正常....请有人知道如何去做...?您的贡献将受到高度赞赏。