1.Create XML View it Contains 2 button upload & Choose an Image view to set selected image to image view an after that upload button to upload on server. <?xml version="1.0" encoding="utf-8"?> <LinearLayout 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="15dp" android:paddingLeft="15dp" android:paddingRight="15dp" android:paddingTop="15dp" android:orientation="vertical"> <ImageView android:layout_width="match_parent" android:layout_height="200dp" android:id="@+id/image"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Choose" android:id="@+id/choose"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Upload" android:id="@+id/upload"/> </LinearLayout>
How It Works?
Image to Base64 String
- First convert the image into bitmap.
- Then compress bitmap to ByteArrayOutputStream.
- Convert ByteArrayOutputStream to byte array.
- Finally convert byte array to base64 string.
Base64 String to Image
- Convert the base64 string to byte array.
- Now convert byte array to bitmap
-
2)Java file for XML biniding.
-
public class MainActivity extends AppCompatActivity { Button choose,upload; ImageView image; int PICK_IMAGE_REQUEST = 111; String URL ="Your API"; Bitmap bitmap; ProgressDialog progressDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); choose=(Button)findViewById(R.id.choose); upload=(Button)findViewById(R.id.upload); image=(ImageView)findViewById(R.id.image); choose.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_PICK); startActivityForResult(Intent.createChooser(intent, "Select Image"), PICK_IMAGE_REQUEST); } }); upload.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { sendImageToServer(); } }); } private void sendImageToServer() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); byte[] imageBytes = baos.toByteArray(); final String imageString = Base64.encodeToString(imageBytes, Base64.DEFAULT); StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() { @Override public void onResponse(String response) { getJsonResponse(response); // Toast.makeText(ReaderActivity.this, response, Toast.LENGTH_LONG).show(); System.out.println("RESPONSE"+response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_LONG).show(); } }) { @Override protected Map<String, String> getParams() { Map<String, String> params = new HashMap<String, String>(); params.put("Comment", "Report"); params.put("FileName",imageString); params.put("FileType", "Profile"); params.put("ReportType","IncidentType"); params.put("SenderId","57"); return params; } }; RequestQueue requestQueue = Volley.newRequestQueue(this); requestQueue.add(stringRequest); } private void getJsonResponse(String response) { try { JSONArray jsonArray = new JSONArray(response); String resultCode = jsonArray.getJSONObject(0).getString("ResultCode"); if(resultCode.equals("OK")) { Toast.makeText(this, "Image Uploaded Successfully....!!!!", Toast.LENGTH_SHORT).show(); }else { Toast.makeText(this, "Please try Again.....!!!!", Toast.LENGTH_SHORT).show(); } } catch (JSONException e) { e.printStackTrace(); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) { Uri filePath = data.getData(); try { //getting image from gallery bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath); //Setting image to ImageView image.setImageBitmap(bitmap); } catch (Exception e) { e.printStackTrace(); } } } }