0
votes

I have been working on trying to take a photo then upload it to my database however once the photo is taken the application crashes. It looks to me like it is a few different problems but I can't seem to figure where they are.

public class MainActivity extends AppCompatActivity {

  private Button button;
  private String encoded_string, image_name;
  private Bitmap bitmap;
  private File file;
  private Uri file_uri;

  @Override
  protected void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    button = (Button) findViewById(R.id.btPhoto);
    button.setOnClickListener(new View.OnClickListener() {

      public void onClick(View view) {

        Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        getFileUri();
        i.putExtra(MediaStore.EXTRA_OUTPUT, file_uri);
        startActivityForResult(i, 10);
      }

    });
  }

  private void getFileUri() {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HH_mm_ss");
    String currentTimeStamp = dateFormat.format(new Date());
    String image_name = "picture_" + currentTimeStamp + ".jpg";

    file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
        + File.separator + image_name
    );

    file_uri = Uri.fromFile(file);
  }

  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);
      bitmap.recycle();

      byte[] array = stream.toByteArray();
      encoded_string = Base64.encodeToString(array, 0);
      return null;
    }

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

  private void makeRequest(){
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    StringRequest request = new StringRequest(Request.Method.POST, "http://www.houstonaduk.com/ImageFile.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 I am receiving at the minute. I have searched to see what is wrong with my code but I am a newbie too so thought Stackoverflow would give me some helpful pointers.

03-23 21:56:14.936 24471-25343/com.app.jools.camera E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/Pictures/picture_20170323_21_56_10.jpg: open failed: EACCES (Permission denied) 03-23 21:56:14.946 24471-25343/com.app.jools.camera E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1 Process: com.app.jools.camera, PID: 24471 java.lang.RuntimeException: An error occurred while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:309) at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354) at java.util.concurrent.FutureTask.setException(FutureTask.java:223) at java.util.concurrent.FutureTask.run(FutureTask.java:242) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) at java.lang.Thread.run(Thread.java:818) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference at com.app.jools.camera.MainActivity$Encode_image.doInBackground(MainActivity.java:89) at com.app.jools.camera.MainActivity$Encode_image.doInBackground(MainActivity.java:84) at android.os.AsyncTask$2.call(AsyncTask.java:295) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) at java.lang.Thread.run(Thread.java:818)

Thank you for any help and guidance.

1

1 Answers

0
votes

This is because you haven't declared permission for writing the storage in your AndroidManifest.xml as the error message tells:

03-23 21:56:14.936 24471-25343/com.app.jools.camera E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/Pictures/picture_20170323_21_56_10.jpg: open failed: EACCES (Permission denied)

So you need to add the permission:

Manifest.permission.WRITE_EXTERNAL_STORAGE  

As of API >= 23, you need to manage the permission in run-time, read more for it at Understanding App Permissions.