Unable to decode stream: java.io.FileNotFoundException:

匿名 (未验证) 提交于 2019-12-03 01:45:01

问题:

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:

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

回答1:

 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.



回答2:

try using BitmapFactory.decodeStream rather than BitmapFactory.decodeFile.

Here is an example of how to use it.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!