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);
tofile_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:
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; }