2010-03-12 71 views
23

我想从Android Uri获取一个字节数组。Android - 从Uri到InputStream到字节数组?

我有以下代码,但它始终告诉我字节数组长度为61字节,即使该文件相当大 - 所以我认为它可能会将Uri 字符串转换为字节数组,比文件:(

Log.d(LOG_TAG, "fileUriString = " + fileUriString); 
    Uri tempuri = Uri.parse(fileUriString); 
    InputStream is = cR.openInputStream(tempuri); 
    String str=is.toString(); 
    byte[] b3=str.getBytes(); 
    Log.d(LOG_TAG, "len of data is " + imageByteArray.length 
    + " bytes"); 

请有人可以帮我找出做什么

输出为 “fileUriString =内容://媒体/外部/视频/媒体/ 53”?和“LEN的数据是61字节“。

谢谢!

+0

什么是'cR'?你问如何“从一个Uri到一个InputStream”,但你已经有了一个'InputStream',而没有说明你是如何得到它的。 – Vince 2015-04-17 17:05:16

+0

@Vince'cR'是ContentResolver类的一个实例 – mbelsky 2015-10-02 11:24:52

回答

57

is.toString()将为您提供InputStream实例的字符串表示形式,而不是其内容。

您需要从InputStream读取()字节到您的数组中。有两种读取方法可以做到这一点,read()一次只读取一个字节,read(byte[] bytes)从InputStream读取字节到您传递给它的字节数组中。


更新:读取因为一个InputStream不具有长度这样的字节,则需要读取的字节中,直到有一无所有。我建议为自己创建一个方法,这是一个很好的简单起点(这是我至少会在Java中执行的方法)。

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

谢谢。虽然这并不能解决问题,因为在我提供它之前我必须指定byte []数组的长度,并且我无法从InputStream获取它。任何想法如何我可以计算给出的Uri数据的长度? – AP257 2010-03-13 11:10:28

+0

对不起,那是不可能的 – user1324936 2012-05-26 22:35:22

+0

你不必提前知道文件大小。建议的解决方案将适用于任何流长度。 – 2013-04-10 14:11:53

2
while ((len = inputStream.read(buffer)) != -1) 

应该

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

如果流具有在this answer解释没有可用的字节该读的方式将不会阻止。

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

下载JAR: http://commons.apache.org/io/download_io.cgi

+0

如果您只收到一半文件,则阻止是正确的行为。检查可用性是检查文件结尾的可怕替代方法。 – 2015-06-18 01:18:58

8

随着阿帕奇百科全书,你可以从一个Stream感谢IOUtils.toByteArray(InputStream)为下一读取所有字节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

InputStreamReader使用Big5编码格式,因为我的内容有中文字符,所以可以使用UTF-8 – user2374654 2013-05-12 10:14:56

0

如果你有一个开放的,而不是一个普通的文件名,你应该使用内容解析器。 Android: Getting a file URI from a content URI?

我试过了,它的工作原理。 Uri uri; //这是一些东西,我从文件选择器中获得。当然,我必须确定,uri指向一个文件名,它不是图像,音频或其他内容......

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(); 

我从代码中省略了一些try-catch-finally,但最重要的是这里。 首先,如果你有一个uri,你不能使用标准的文件阅读器。