Android上传图片至java服务器

时间:2024-11-17 15:03:37
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import .; import ; import .Base64; import ; import ; import ; import ; import ; import ; import ; import ; import ; @SuppressLint("NewApi") public class UploadActivity extends AppCompatActivity implements View.OnClickListener { private EditText editTextName; private ProgressDialog prgDialog; private int RESULT_LOAD_IMG = 1; private RequestParams params = new RequestParams(); private String encodedString; private Bitmap bitmap; private String imgPath; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(.activity_main); prgDialog= new ProgressDialog(this); (false); editTextName = (EditText) findViewById(); findViewById(.choose_image).setOnClickListener(this); findViewById(.upload_image).setOnClickListener(this); } @Override public void onClick(View view) { switch (()) { case .choose_image: loadImage(); break; case .upload_image: uploadImage(); break; } } public void loadImage() { //这里就写了从相册中选择图片,相机拍照的就略过了 Intent galleryIntent = new Intent(Intent.ACTION_PICK, .EXTERNAL_CONTENT_URI); startActivityForResult(galleryIntent, RESULT_LOAD_IMG); } //当图片被选中的返回结果 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); try { if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK && null != data) { Uri selectedImage = (); String[] filePathColumn = { }; // 获取游标 Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); (); int columnIndex = (filePathColumn[0]); imgPath = (columnIndex); (); ImageView imgView = (ImageView) findViewById(); ((imgPath)); } else { (this, "You haven't picked Image", Toast.LENGTH_LONG).show(); } } catch (Exception e) { (this, "Something went wrong", Toast.LENGTH_LONG).show(); } } //开始上传图片 private void uploadImage() { if (imgPath != null && !()) { ("Converting Image to Binary Data"); (); encodeImagetoString(); } else { (getApplicationContext(), "You must select image from gallery before you try to upload", Toast.LENGTH_LONG).show(); } } public void encodeImagetoString() { new AsyncTask<Void, Void, String>() { protected void onPreExecute() { }; @Override protected String doInBackground(Void... params) { options = null; options = new (); = 3; bitmap = (imgPath, options); ByteArrayOutputStream stream = new ByteArrayOutputStream(); // 压缩图片 (, 50, stream); byte[] byte_arr = (); // Base64图片转码为String encodedString = (byte_arr, 0); return ""; } @Override protected void onPostExecute(String msg) { ("Calling Upload"); // 将转换后的图片添加到上传的参数中 ("image", encodedString); ("filename", ().toString()); // 上传图片 imageUpload(); } }.execute(null, null, null); } public void imageUpload() { ("Invoking JSP"); String url = "http://172.18.2.73:8080/upload/"; AsyncHttpClient client = new AsyncHttpClient(); (url, params, new AsyncHttpResponseHandler() { @Override public void onSuccess(int statusCode, Header[] headers, byte[] bytes) { (); (getApplicationContext(), "upload success", Toast.LENGTH_LONG).show(); } @Override public void onFailure(int statusCode, Header[] headers, byte[] bytes, Throwable throwable) { (); if (statusCode == 404) { (getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show(); } // 当 Http 响应码'500' else if (statusCode == 500) { (getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show(); } // 当 Http 响应码 404, 500 else { ( getApplicationContext(), "Error Occured n Most Common Error: n1. Device " + "not connected to Internetn2. Web App is not deployed in App servern3." + " App server is not runningn HTTP Status code : " + statusCode, Toast.LENGTH_LONG).show(); } } }); } @Override protected void onDestroy() { super.onDestroy(); if (prgDialog != null) { prgDialog .dismiss(); } } }