Android - getting from a Uri to an InputStream to a byte array?

前端 未结 7 2047
慢半拍i
慢半拍i 2020-11-30 03:44

I\'m trying to get from an Android Uri to a byte array.

I have the following code, but it keeps telling me that the byte array is 61 bytes long, even though the fil

7条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-30 04:26

    Sharing my idea :D

    private static byte[] getStringFromInputStream(InputStream is) 
    {
    
        BufferedReader br = null;
        StringBuilder sb = new StringBuilder();
        byte[] bReturn = new byte[0];
    
        String line;
        try 
        {
    
            br = new BufferedReader(new InputStreamReader(is, "Big5"));
            while ((line = br.readLine()) != null) 
            {
                sb.append(line);
            }
            String sContent = sb.toString();
            bReturn = sContent.getBytes();          
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        } 
        finally 
        {
            if (br != null) 
            {
                try 
                {
                    br.close();
                } 
                catch (IOException e) 
                {
                    e.printStackTrace();
                }
            }
        } 
        return bReturn;
    }
    

提交回复
热议问题