1.添加权限(访问网络权限和读写权限)
2.获取上传文件路径并判断是否为空
3.若不为空,创建异步请求对象
4.创建上传文件路径
5.执行post请求(指定url路径,封装上传参数,新建AsyncHttpResponseHandler方法)
运行效果如下:
在本地文件夹中查看是否获取到图片,如下图显示
重点代码:均有详细解析,请认真查看注释
1、在AndroidManifest.xml中添加权限
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
2、布局文件activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="文件上传" /> <EditText android:id="@+id/et_upload" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:ems="10" android:text="/storage/sdcard0/1.jpg"> <requestFocus /> </EditText> <Button android:id="@+id/btn_upload" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/et_upload" android:onClick="upload" android:text="上传文件" /> </RelativeLayout>
3、MainActivity.java
package com.example.android_upload; import java.io.File; import org.apache.http.Header; import android.app.Activity; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.EditText; import android.widget.Toast; import com.loopj.android.http.AsyncHttpClient; import com.loopj.android.http.AsyncHttpResponseHandler; import com.loopj.android.http.RequestParams; public class MainActivity extends Activity { private EditText et_file; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取控件 et_file = (EditText) findViewById(R.id.et_upload); } //点击上传按钮 public void upload(View v) { int id = v.getId(); switch (id) { case R.id.btn_upload: //获取上传文件的路径 String path = et_file.getText().toString(); //判断上次路径是否为空 if (TextUtils.isEmpty(path.trim())) { Toast.makeText(this, "上次文件路径不能为空", 1).show(); } else { //异步的客户端对象 AsyncHttpClient client = new AsyncHttpClient(); //指定url路径 String url = "http://172.16.237.144:8080/Login/UploadServlet"; //封装文件上传的参数 RequestParams params = new RequestParams(); //根据路径创建文件 File file = new File(path); try { //放入文件 params.put("profile_picture", file); } catch (Exception e) { // TODO: handle exception System.out.println("文件不存在----------"); } //执行post请求 client.post(url,params, new AsyncHttpResponseHandler() { @Override public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) { if (statusCode == 200) { Toast.makeText(getApplicationContext(), "上次成功", 1) .show(); } } @Override public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) { error.printStackTrace(); } }); } break; default: break; } } }
重点代码就是这些,自己动手查看一下效果吧!~
开源框架资源:http://download.csdn.net/detail/zhaoyazhi2129/7400787
源码:http://download.csdn.net/detail/zhaoyazhi2129/7400811
转发请标明原文地址http://blog.csdn.net/zhaoyazhi2129/article/details/27048149