0

I have an app that takes pictures. I have recently had to change part of said app due to no longer being able pass URIs in intents. So I changed part of my code from

file_uri = Uri.fromFile(file);

to

file_uri = FileProvider.getUriForFile(PhotoActivity.this,BuildConfig.APPLICATION_ID + ".provider", file);

however now this seems to have broken another part of my code and I cant figure out how to fix it.

This is the relevant parts:

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

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

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    StringRequest request = new StringRequest(Request.Method.POST, POST_URL,
            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);
}

private void getFileUri() {

    String uuid = UUID.randomUUID().toString();
    imageName=uuid;

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

    //file_uri = Uri.fromFile(file);
    file_uri = FileProvider.getUriForFile(PhotoActivity.this,
            BuildConfig.APPLICATION_ID + ".provider",
            file);
}

and this is the error I'm getting:

E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /external_files/Pictures/aba094a2-3f15-405f-9203-fdc4245dc003.jpg (No such file or directory)
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
                                                                                 Process: com.example.gabrieluliano.my_sick_app, PID: 8639
                                                                                 java.lang.RuntimeException: An error occurred while executing doInBackground()
                                                                                     at android.os.AsyncTask$3.done(AsyncTask.java:318)
                                                                                     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:243)
                                                                                     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
                                                                                     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
                                                                                     at java.lang.Thread.run(Thread.java:762)
                                                                                  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.example.gabrieluliano.my_sick_app.PhotoActivity$Encode_image.doInBackground(PhotoActivity.java:347)
                                                                                     at com.example.gabrieluliano.my_sick_app.PhotoActivity$Encode_image.doInBackground(PhotoActivity.java:331)
                                                                                     at android.os.AsyncTask$2.call(AsyncTask.java:304)
                                                                                     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                                     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243) 
                                                                                     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 
                                                                                     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 
                                                                                     at java.lang.Thread.run(Thread.java:762) 

Anyone know whats wrong with it? thanks

edit: I changed the bitmap part to:

try(InputStream is = new URL( file_uri.getPath() ).openStream()){
        bitmap = BitmapFactory.decodeStream(is);

        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;
        }
        catch(Exception e){
            return null;
        }
Gabriel
  • 69
  • 1
  • 9
  • Are you positive the file or directory exists.... ` /external_files/Pictures/aba094a2-3f15-405f-9203-fdc4245dc003.jpg (No such file or directory)` – Dayan Mar 06 '17 at 13:32
  • the directory is the phone's default pictures directory with the newly taken image name – Gabriel Mar 06 '17 at 13:39

2 Answers2

3
 bitmap = BitmapFactory.decodeFile(file_uri.getPath());

change to

 bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(file_uri));

But of course it is not good to make a Bitmap out of the file first.

You should of course upload the file 'as is' without messing around with Bitmaps and BitmapFactory.

greenapps
  • 11,154
  • 2
  • 16
  • 19
  • thanks this works, and I'm using it because I just wanna send the base64 representation of the image to my backend – Gabriel Mar 06 '17 at 14:16
0

try using BitmapFactory.decodeStream rather than BitmapFactory.decodeFile.

Here is an example of how to use it.

Community
  • 1
  • 1
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
  • It stopped the app from crashing but now I'm getting this – Gabriel Mar 06 '17 at 13:26
  • E/Volley: [103] NetworkDispatcher.run: Unhandled exception java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference – Gabriel Mar 06 '17 at 13:27
  • that's explanatory, however, since you're using AsyncTask I'd recommend you to look at similar problems within StackOverflow. – Ousmane D. Mar 06 '17 at 13:32
  • sorry to bother again, but i think the problem is that on the example you linked me to they are using file_url as the parameter for InputStream and I tried to do file_uri.getPath() any idea of what should be there as I dont actually have a image url? – Gabriel Mar 06 '17 at 13:45
  • @GabrielUliano I am really busy at the moment, sorry but once I finish what I am currently doing, I will get back to you as soon as possible if you don't mind. Thanks. – Ousmane D. Mar 06 '17 at 13:49