Andrew T Andrew T - 1 month ago 20
Java Question

Android bitmap compress error

I am trying to take a photo from android and post it to the server but when I run the app, I can access the camera, take a photo and get a crash after accepting the photo, but the app should convert the image to base64, and send it to the server. Why? (The error is in the line 79:Encode_image class: bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);

The crash error is :

E/BitmapFactory: Unable to decode stream: /storage/emulated/0/test123.jpg: open failed: EACCES (Permission denied)
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: com.example.andrei.sendphotos, PID: 3870
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(
at java.util.concurrent.FutureTask.finishCompletion(
at java.util.concurrent.FutureTask.setException(
at android.os.AsyncTask$SerialExecutor$
at java.util.concurrent.ThreadPoolExecutor.runWorker(
at java.util.concurrent.ThreadPoolExecutor$
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean$CompressFormat, int,' on a null object reference
at com.example.andrei.sendphotos.MainActivity$Encode_image.doInBackground(
at com.example.andrei.sendphotos.MainActivity$Encode_image.doInBackground(
at android.os.AsyncTask$

Here is my code:

public class MainActivity extends AppCompatActivity {

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

protected void onCreate(Bundle savedInstanceState) {

takePhotoButton = (Button) findViewById(;
takePhotoButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, file_uri);
startActivityForResult(i, 10);

private void getFileUri() {
image_name= "test123.jpg";
//file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + File.separator + image_name);
file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + 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>{

protected Void doInBackground(Void... params) {

bitmap = BitmapFactory.decodeFile(file_uri.getPath());
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);

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

protected void onPostExecute(Void aVoid) {

private void makeRequest() {
RequestQueue requestQueue = Volley.newRequestQueue(this);
StringRequest request = new StringRequest(Request.Method.POST, "",

new Response.Listener<String>() {
public void onResponse(String response) {

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

}) {
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;



The problem may be because you are getting the actual path, but you need the real path