BitmapFactory: Unable to decode stream: java.io.FileNotFoundException even when file IS actually there

前端 未结 4 1818
刺人心
刺人心 2021-01-02 04:28

I\'m creating a simple app to take a picture. this is my code

Button b1;
ImageView iv;
String TAG = \"MAIN ACTIVITY\";

File photo;
private Uri mImageUri;


         


        
相关标签:
4条回答
  • 2021-01-02 04:49
    file:///storage/emulated/0/cameratest/picture459838058.jpg
    

    Remove file:// because the decodeFile() expects a file system path.

    /storage/emulated/0/cameratest/picture459838058.jpg
    
    0 讨论(0)
  • 2021-01-02 04:51

    Replace mImageUri.toString() with mImageUri.getPath().

    decodeFile expects a path, not an uri string.

    0 讨论(0)
  • 2021-01-02 05:03

    Ok for me it was the file path was wrong so I needed to get the real filepath.

    First

    File file = new File(getPath(uri));
    
    public String getPath (Uri uri)
    {
        String[] projection = {MediaStore.Images.Media.DATA};
        Cursor cursor = getContentResolver().query(uri,
                                                   projection,
                                                   null,
                                                   null,
                                                   null);
        if (cursor == null)
            return null;
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        String s = cursor.getString(column_index);
        cursor.close();
        return s;
    }
    

    Then Back To Uri

    Uri newUri = Uri.fromFile(file);
    

    This conversion to file and back to uri did the trick for me. I was receiving simple data from action.SEND.

    0 讨论(0)
  • 2021-01-02 05:08

    Use BitmapFactory.decodeStream instead of BitmapFactory.decodeFile.

    try ( InputStream is = new URL( file_url ).openStream() ) {
      Bitmap bitmap = BitmapFactory.decodeStream( is );
    }
    

    Source https://stackoverflow.com/a/28395036/5714364

    0 讨论(0)
提交回复
热议问题