Meet Sheth
Meet Sheth

Reputation: 1

Android Image capture using URI and upload to MySQL database via PHP

I have made an android app to capture image from the default camera, and then compress it using bitmap.compress() and then upload it to my MySQL database using PHP. Now the issue is, using Bitmap I get very low quality image. I found a solution that is to use URI to capture image instead of Bitmap. But then I don't know how to use URI to 'upload it to mysql via php'. Can somebody please edit my code from Bitmap to URI and output me a 'image string' (just like 'encoded_front' string variable used in the code) which I can pass to my php script. Thanks a ton.

Here's my code

front_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Dexter.withContext(getApplicationContext())
                        .withPermission(Manifest.permission.CAMERA)
                        .withListener(new PermissionListener() {
                            @Override
                            public void onPermissionGranted(PermissionGrantedResponse permissionGrantedResponse) {
                                Intent front_image=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                                startActivityForResult( front_image,111);
                            }

                            @Override
                            public void onPermissionDenied(PermissionDeniedResponse permissionDeniedResponse) {

                            }

                            @Override
                            public void onPermissionRationaleShouldBeShown(PermissionRequest permissionRequest, PermissionToken permissionToken) {
                                permissionToken.continuePermissionRequest();
                            }
                        }).check();
            }
        });
upload_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                insert_db();
            }
        });
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if(requestCode==111 && resultCode==RESULT_OK)
        {
            bitmap=(Bitmap)data.getExtras().get("data");
            encodebitmap(bitmap,1);
        }
        
        super.onActivityResult(requestCode, resultCode, data);
    }
private void encodebitmap(Bitmap bitmap,int i)
    {
        if(i==1)
        {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);

            byte[] byteofimages = byteArrayOutputStream.toByteArray();
            encoded_front = android.util.Base64.encodeToString(byteofimages, Base64.DEFAULT);
// encoded_front is a global string variable

        }
private void insert_db()
    {
 StringRequest request=new StringRequest(Request.Method.POST, apiurl, new Response.Listener<String>() {
            @Override
            public void onResponse(String response)
            {
                
                
                Toast.makeText(getApplicationContext(),response.toString(),Toast.LENGTH_LONG).show();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error)
            {
                Toast.makeText(getApplicationContext(),error.toString(),Toast.LENGTH_LONG).show();
            }
        })
        {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError
            {
                Map<String,String> map=new HashMap<String, String>();
               
                map.put("front_face",encoded_front);
                
                return map;
            }
        };

        RequestQueue queue= Volley.newRequestQueue(getApplicationContext());
        queue.add(request);
        


    }  // end of function

Upvotes: 0

Views: 165

Answers (0)

Related Questions