How to Upload a Photo From Gallery Android Studio
Hey in this tutorial I am Sharing Android Image UpLoad to server. How to use a camera to capture an image and gallery to pick an image and choice convert to bitmap and upload to the server. Information technology very is and simpler to use the camera in android and upload the prototype to the server. Select the prototype to upload to the server. Android Capture image from a camera and Pick Image from the gallery and Android image upload to server.
- Android Image UpLoad to server.
- Android how to upload the epitome to the server.
- how to click the prototype with the camera.
- Android movie epitome from the gallery.
- Android Paradigm UpLoad to server with the photographic camera.
- Android Image UpLoad to server with gallery.
- Android Image UpLoad to server with volley network library.
- Android image upload image from gallery and camera.
- Android Epitome UpLoad to server tutorial.
- Android Epitome UpLoad to server web log.
- Android Prototype UpLoad to server Article.
Android Image UpLoad to server.
There is a elementary step to follow and implement these . and I have to use a volley network library. Here I create a simple activeness and add together an image view and button Ui. When you lot click on the Button open a dialog for the selected image from the gallery and camera option. And subsequently selecting an epitome, we telephone call API for Android image upload to server.
Stride 1: Start Android Studio and Create a Project in your Android studio
Setp2:- Create an Activity Uplode_Reg_Photo
Step 3:- Open your Manifest file and add user permission
< uses-permission android :name= "android.permission.Cyberspace" />
< uses-permission android :name= "android.permission.ACCESS_NETWORK_STATE" />
< uses-permission android :name= "android.permission.WRITE_EXTERNAL_STORAGE" />
< uses-permission android :proper noun= "android.permission.READ_EXTERNAL_STORAGE" />
< uses-permission android :proper noun= "android.permission.Photographic camera" />
Android Prototype UpLoad to server From Camera and gallery
Footstep iv:- Open up your activity_ uplode_reg_photo.xml file Android Image UpLoad to the server.
activity_ uplode_reg_photo XML File
<? xml version= "i.0" encoding= "utf-8" ?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: app = "http://schemas.android.com/apk/res-auto" xmlns: tools = "http://schemas.android.com/tools" android :layout_width= "match_parent" android :layout_height= "match_parent" android :padding= "15dp" android :orientation= "vertical" tools :context= "com.codeplayon.Uplode_Reg_Photo"> <TextView android :layout_width= "match_parent" android :layout_height= "wrap_content" android :layout_marginTop= "20dp" android :text= " Upload id prof pic"/> <ImageView android :id= "@+id/IdProf" android :layout_width= "match_parent" android :layout_height= "250dp" android :src= "@drawable/ic_insert_photo_white_48dp" android :layout_marginTop= "10dp" android :background= "@drawable/editbox"/> <Button android :id= "@+id/UploadBtn" android :layout_width= "180dp" android :layout_height= "wrap_content" android :layout_marginTop= "15dp" android :text= "Upload" android :textStyle= "assuming" android :textAppearance= "?android:textAppearanceMedium" android :textColor= "@color/white" android :layout_gravity= "center" android :groundwork= "@drawable/push"/> </LinearLayout>
Android Image UpLoad to server
In this java file, we implement the functional on image click prototype from photographic camera and Pick an image from gallery and upload image to the server. So that follow the steps for Android Image UpLoad to server.
Step v:- Open Your java file
public class Uplode_Reg_Photo extends AppCompatActivity implements View.OnClickListener{ public static final String KEY_User_Document1 = "doc1"; ImageView IDProf; Button Upload_Btn; private String Document_img1="" @Override protected void onCreate(Packet savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout. activity_uplode__reg__photo ); IDProf=(ImageView)findViewById(R.id. IdProf ); Upload_Btn=(Push)findViewById(R.id. UploadBtn ); IDProf.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { selectImage(); } }); Upload_Btn.setOnClickListener(this); } private void selectImage() { terminal CharSequence[] options = { "Accept Photo", "Choose from Gallery","Cancel" }; android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(Uplode_Reg_Photo.this); builder.setTitle("Add together Photo!"); builder.setItems(options, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int item) { if (options[particular].equals("Accept Photo")) { Intent intent = new Intent(MediaStore. ACTION_IMAGE_CAPTURE ); File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg"); intent.putExtra(MediaStore. EXTRA_OUTPUT , Uri.fromFile(f)); startActivityForResult(intent, 1); } else if (options[item].equals("Choose from Gallery")) { Intent intent = new Intent(Intent. ACTION_PICK ,android.provider.MediaStore.Images.Media. EXTERNAL_CONTENT_URI ); startActivityForResult(intent, 2); } else if (options[item].equals("Abolish")) { dialog.dismiss(); } } }); builder.show(); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK ) { if (requestCode == ane) { File f = new File(Environment.getExternalStorageDirectory().toString()); for (File temp : f.listFiles()) { if (temp.getName().equals("temp.jpg")) { f = temp; suspension; } } effort { Bitmap bitmap; BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(), bitmapOptions); bitmap=getResizedBitmap(bitmap, 400); IDProf.setImageBitmap(bitmap); BitMapToString(bitmap); String path = android.bone.Environment .getExternalStorageDirectory() + File. separator + "Phoenix" + File. separator + "default"; f.delete(); OutputStream outFile = nil; File file = new File(path, String.valueOf(Arrangement.currentTimeMillis()) + ".jpg"); effort { outFile = new FileOutputStream(file); bitmap.shrink(Bitmap.CompressFormat. JPEG , 85, outFile); outFile.flush(); outFile.close(); } catch (FileNotFoundException east) { due east.printStackTrace(); } catch (IOException e) { east.printStackTrace(); } take hold of (Exception e) { e.printStackTrace(); } } grab (Exception e) { e.printStackTrace(); } } else if (requestCode == 2) { Uri selectedImage = information.getData(); Cord[] filePath = { MediaStore.Images.Media. DATA }; Cursor c = getContentResolver().query(selectedImage,filePath, null, nil, null); c.moveToFirst(); int columnIndex = c.getColumnIndex(filePath[0]); String picturePath = c.getString(columnIndex); c.shut(); Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath)); thumbnail=getResizedBitmap(thumbnail, 400); Log.w("path of image from gallery......******************.........", picturePath+""); IDProf.setImageBitmap(thumbnail); BitMapToString(thumbnail); } } } public String BitMapToString(Bitmap userImage1) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); userImage1.compress(Bitmap.CompressFormat. PNG , threescore, baos); byte[] b = baos.toByteArray(); Document_img1 = Base64.encodeToString(b, Base64. DEFAULT ); render Document_img1; } public Bitmap getResizedBitmap(Bitmap image, int maxSize) { int width = epitome.getWidth(); int peak = image.getHeight(); float bitmapRatio = (float)width / (float) peak; if (bitmapRatio > 1) { width = maxSize; height = (int) (width / bitmapRatio); } else { acme = maxSize; width = (int) (pinnacle * bitmapRatio); } return Bitmap.createScaledBitmap(image, width, meridian, true); } individual void SendDetail() { final ProgressDialog loading = new ProgressDialog(Uplode_Reg_Photo.this); loading.setMessage("Please Wait..."); loading.show(); loading.setCanceledOnTouchOutside(false); RetryPolicy mRetryPolicy = new DefaultRetryPolicy(0, DefaultRetryPolicy. DEFAULT_MAX_RETRIES , DefaultRetryPolicy. DEFAULT_BACKOFF_MULT ); StringRequest stringRequest = new StringRequest(Request.Method. POST , ConfiURL. Registration_URL , new Response.Listener<String>() { @Override public void onResponse(String response) { try { loading.dismiss(); Log.d("JSON", response); JSONObject eventObject = new JSONObject(response); Cord error_status = eventObject.getString("error"); if (error_status.equals("true")) { Cord error_msg = eventObject.getString("msg"); ContextThemeWrapper ctw = new ContextThemeWrapper( Uplode_Reg_Photo.this, R.style. Theme_AlertDialog ); final AlertDialog.Architect alertDialogBuilder = new AlertDialog.Builder(ctw); alertDialogBuilder.setTitle("Vendor Detail"); alertDialogBuilder.setCancelable(false); alertDialogBuilder.setMessage(error_msg); alertDialogBuilder.setPositiveButton("ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { } }); alertDialogBuilder.show(); } else { String error_msg = eventObject.getString("msg"); ContextThemeWrapper ctw = new ContextThemeWrapper( Uplode_Reg_Photo.this, R.style. Theme_AlertDialog ); final AlertDialog.Architect alertDialogBuilder = new AlertDialog.Builder(ctw); alertDialogBuilder.setTitle("Registration"); alertDialogBuilder.setCancelable(false); alertDialogBuilder.setMessage(error_msg); // alertDialogBuilder.setIcon(R.drawable.doubletick); alertDialogBuilder.setPositiveButton("ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { Intent intent=new Intent(Uplode_Reg_Photo.this,Log_In.grade); startActivity(intent); finish(); } }); alertDialogBuilder.show(); } }catch(Exception e){ Log.d("Tag", e.getMessage()); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError mistake) { loading.dismiss(); if (fault instanceof TimeoutError || error instanceof NoConnectionError) { ContextThemeWrapper ctw = new ContextThemeWrapper( Uplode_Reg_Photo.this, R.style. Theme_AlertDialog ); final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ctw); alertDialogBuilder.setTitle("No connection"); alertDialogBuilder.setMessage(" Connection time out error please attempt again "); alertDialogBuilder.setPositiveButton("ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { } }); alertDialogBuilder.prove(); } else if (error instanceof AuthFailureError) { ContextThemeWrapper ctw = new ContextThemeWrapper( Uplode_Reg_Photo.this, R.way. Theme_AlertDialog ); concluding AlertDialog.Architect alertDialogBuilder = new AlertDialog.Builder(ctw); alertDialogBuilder.setTitle("Connection Error"); alertDialogBuilder.setMessage(" Authentication failure connection error please try over again "); alertDialogBuilder.setPositiveButton("ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { } }); alertDialogBuilder.show(); // TODO } else if (error instanceof ServerError) { ContextThemeWrapper ctw = new ContextThemeWrapper( Uplode_Reg_Photo.this, R.style. Theme_AlertDialog ); terminal AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ctw); alertDialogBuilder.setTitle("Connexion Error"); alertDialogBuilder.setMessage("Connection fault delight effort again"); alertDialogBuilder.setPositiveButton("ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { } }); alertDialogBuilder.show(); // TODO } else if (error instanceof NetworkError) { ContextThemeWrapper ctw = new ContextThemeWrapper( Uplode_Reg_Photo.this, R.style. Theme_AlertDialog ); concluding AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ctw); alertDialogBuilder.setTitle("Connection Error"); alertDialogBuilder.setMessage("Network connection error please try again"); alertDialogBuilder.setPositiveButton("ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { } }); alertDialogBuilder.show(); // TODO } else if (error instanceof ParseError) { ContextThemeWrapper ctw = new ContextThemeWrapper( Uplode_Reg_Photo.this, R.fashion. Theme_AlertDialog ); concluding AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ctw); alertDialogBuilder.setTitle("Error"); alertDialogBuilder.setMessage("Parse fault"); alertDialogBuilder.setPositiveButton("ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { } }); alertDialogBuilder.evidence(); } // Toast.makeText(Login_Activity.this,fault.toString(), Toast.LENGTH_LONG ).show(); } }){ @Override protected Map<String, String> getParams() throws AuthFailureError { Map<String,Cord> map = new HashMap<Cord,Cord>(); map.put( KEY_User_Document1 ,Document_img1); return map; } }; RequestQueue requestQueue = Volley.newRequestQueue(this); stringRequest.setRetryPolicy(mRetryPolicy); requestQueue.add(stringRequest); } @Override public void onClick(View 5) { if (Document_img1.equals("") || Document_img1.equals(null)) { ContextThemeWrapper ctw = new ContextThemeWrapper( Uplode_Reg_Photo.this, R.style. Theme_AlertDialog ); final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ctw); alertDialogBuilder.setTitle("Id Prof Can't Empty "); alertDialogBuilder.setMessage("Id Prof Can't empty delight select whatever one document"); alertDialogBuilder.setPositiveButton("ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { } }); alertDialogBuilder.show(); return; } else{ if (AppStatus.getInstance(this).isOnline()) { SendDetail(); // Toast.makeText(this,"You lot are online!!!!",Toast.LENGTH_LONG).show(); } else { Toast.makeText(this,"You lot are not online!!!!",Toast. LENGTH_LONG ).show(); Log.5("Home", "############################Y'all are not online!!!!"); } } } }
Android Image UpLoad to server From Camera and gallery.
Also, yous can learn more than about other android solutions and Android tutorials. Just click on Read More Android Tutorial . Codeplayon Github free source code and instance for android and palpitate app evolution.
mcleoddespassoling1956.blogspot.com
Source: https://www.codeplayon.com/2018/11/android-image-upload-to-server-from-camera-and-gallery/
0 Response to "How to Upload a Photo From Gallery Android Studio"
Enregistrer un commentaire