Post Image data in android studio to a server url not working on android 4.1.2

75
January 12, 2021, at 6:50 PM

I am taking a screenshot on my android app using this code:

       Long tsLong = System.currentTimeMillis()/1000;
                final String ts = tsLong.toString();
               
                String mPath =Environment.getExternalStorageDirectory().getAbsolutePath() + "/Download/ScreenShot/" + ts + ".jpg";
                // create bitmap screen capture
                View v1 = main;
                v1.setDrawingCacheEnabled(true);
                final Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
                v1.setDrawingCacheEnabled(false);
                File imageFile = new File(mPath);
                FileOutputStream outputStream = new FileOutputStream(imageFile);
                int quality = 100;
                bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
                outputStream.flush();
                outputStream.close();

I am then sending this screenshot to a server using the following:

 String url = "http://website.com/";
            StringRequest MyStringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Log.e("takeScreenshot", "onResponse: "+response);
                }
            }, new Response.ErrorListener() { //Create an error listener to handle errors appropriately.
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("takeScreenshot", "error: "+error);
                    //This code is executed if there is an error.
                }
            }) {
                protected Map<String, String> getParams() {
                    Map<String, String> MyData = new HashMap<String, String>();
                    MyData.put("image_tag", ts);
                    MyData.put("image_data", getStringImage(bitmap));
                    return MyData;
                }
            };
            RequestQueue requestQueue = Volley.newRequestQueue(SecondActivity.this);
            RetryPolicy mRetryPolicy = new DefaultRetryPolicy(0, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
            MyStringRequest.setRetryPolicy(mRetryPolicy);
            requestQueue.add(MyStringRequest);

This code works fine on any android that is 5.0+ but not on the 4.1.2 that I am using, for some reason this code takes the screenshot and tries to send the post request but doesn't and it does not enter the onResponse or onErrorResponse

I have added this RetryPolicy mRetryPolicy = new DefaultRetryPolicy(0, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT); MyStringRequest.setRetryPolicy(mRetryPolicy); to try and fix my problem but it did nothing

READ ALSO
Android studio Build failed with NDK

Android studio Build failed with NDK

found error while build the project with NDK

80
How to make an extensive Website with 100s pf pages like w3school? [closed]

How to make an extensive Website with 100s pf pages like w3school? [closed]

Want to improve this question? Add details and clarify the problem by editing this post

16
Cannot connect to MySQL server in Dreamweaver MX 2004

Cannot connect to MySQL server in Dreamweaver MX 2004

I am trying in Dreamweaver MX 2004 to connect to MySQL database

37