0
votes

This is my code about Android upload image using Android Studio. My apps crash after capturing image (the apps was stopped working) but it is doing fine in virtual emulator (same API 19), and the logcat tell me an error occur while executing doing background. Can someone help me find out my mistake?

 protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

            button = (Button)findViewById(R.id.start);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    getFileUri();
                    i.putExtra(MediaStore.EXTRA_OUTPUT,file_uri);
                    startActivityForResult(i,10);
                }
            });
        }
    private void getFileUri() {
            image_name = "testing123.jpg";
            file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
                    +File.separator + image_name);
            file_uri = Uri.fromFile(file);
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
             if(requestCode == 10 && resultCode==RESULT_OK)
             {
                new Encode_image().execute();
             }
        }


        private class Encode_image extends AsyncTask<Void,Void,Void>{
            @Override
            protected Void doInBackground(Void... voids) {
                bitmap = BitmapFactory.decodeFile(file_uri.getPath());
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG,100,stream);
                byte[] array = stream.toByteArray();
                encoded_string = Base64.encodeToString(array,0);
                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                makeRequest();
            }
        }

        private void makeRequest() {
            RequestQueue requestqueue = Volley.newRequestQueue(this);
            StringRequest request = new StringRequest(Request.Method.POST, "http://mygplobang.com/tut_connection.php",
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {

                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            }){
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    HashMap<String,String> map = new HashMap<>();
                    map.put("encoded_string",encoded_string);
                    map.put("image_name",image_name);

                    return map;
                }
            };
            requestqueue.add(request);
        }

This is the error output:

FATAL EXCEPTION: AsyncTask #1 Process: com.example.jeffreykhang.kas, PID: 6879 java.lang.RuntimeException: An error occured while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:300) at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) at java.util.concurrent.FutureTask.setException(FutureTask.java:222) at java.util.concurrent.FutureTask.run(FutureTask.java:242) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:841) Caused by: java.lang.NullPointerException at com.example.jeffreykhang.kas.MainActivity$Encode_image.doInBackground(MainActivity.java:93) at com.example.jeffreykhang.kas.MainActivity$Encode_image.doInBackground(MainActivity.java:90) at android.os.AsyncTask$2.call(AsyncTask.java:288) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)  at java.lang.Thread.run(Thread.java:841)

1
what do u have in line 93 in MainActivity? Makesure nothing is null in doInBackgroundRaghavendra
hi Raghavendra, the line 90 is private class Encode_image extends AsyncTask<Void,Void,Void>{, and line 93 is bitmap = BitmapFactory.decodeFile(file_uri.getPath());JIa FEi
then I guess file_uri is null. Please do a null checkRaghavendra
i am new in android , can u gv me a null check example , at where do i locate the null check ? , at protected void onCreate{} ?JIa FEi
Try this remove return null; in doInBackground() and try once.Raghavendra

1 Answers

0
votes

if you see , you can take photos with the front camera, but the app will crash if you take them with the back camera

the solution is here

bitmap.compress(Bitmap.CompressFormat.JPEG,100,stream);

change bitmap.compress from 100 , to 75

this should fix the problem.