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

前端 未结 7 2021
慢半拍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:15
    while ((len = inputStream.read(buffer)) != -1)
    

    should be

    while (inputStream.available() >0 && (len = inputStream.read(buffer)) != -1)
    

    This way read will not block if stream has no available bytes as explained in this answer.

    0 讨论(0)
  • 2020-11-30 04:17

    With Google Guava, you can use ByteStreams.toByteArray(InputStream) to get all the bytes in the input stream:

    InputStream is = ...;
    byte[] bytes = ByteStream.toByteArray(is);
    
    0 讨论(0)
  • 2020-11-30 04:23

    if You have an Uri, instead of a regular file name, you should use Content Resolver. Android: Getting a file URI from a content URI?

    I tried this, and it works. Uri uri; // it is something, I've got from a file chooser. Of course, I must be sure, that the uri points to a filename, and it is not an image, or audio, or something else...

    InputStream is=getContentResolver().openInputStream(uri);
    InputStreamReader ir=new InputStreamReader(is);
    bu=new BufferedReader(ir);
    String s;
    while ((s=bu.readLine())!=null){
        //do something with the line...
    }
    bu.close();
    

    I've omitted some try-catch-finally from the code, but the most important things are here. The first thing is, that if you have an uri, you cannot use the standard file reader.

    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 2020-11-30 04:27

    is.toString() will give you a String representation of the InputStream instance, not its content.

    You need to read() bytes from the InputStream into your array. There's two read methods to do that, read() which reads a single byte at a time, and read(byte[] bytes) which reads bytes from the InputStream into the byte array you pass to it.


    Update: to read the bytes given that an InputStream does not have a length as such, you need to read the bytes until there is nothing left. I suggest creating a method for yourself something like this is a nice simple starting point (this is how I would do it in Java at least).

    public byte[] readBytes(InputStream inputStream) throws IOException {
      // this dynamically extends to take the bytes you read
      ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
    
      // this is storage overwritten on each iteration with bytes
      int bufferSize = 1024;
      byte[] buffer = new byte[bufferSize];
    
      // we need to know how may bytes were read to write them to the byteBuffer
      int len = 0;
      while ((len = inputStream.read(buffer)) != -1) {
        byteBuffer.write(buffer, 0, len);
      }
    
      // and then we can return your byte array.
      return byteBuffer.toByteArray();
    }
    
    0 讨论(0)
  • 2020-11-30 04:27

    With Apache Commons, you can read all the bytes from a Stream thanks to IOUtils.toByteArray(InputStream) as next:

    byte[] recordData = IOUtils.toByteArray(inStream);
    

    download jar: http://commons.apache.org/io/download_io.cgi

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