Android - getting from a Uri to an InputStream to a byte array?
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();
}
Read file to byte array using uri
As pskink said, your main problem is that you are wrapping your InputStream in a Reader. A Reader is meant to decode bytes into characters, which is, by defintion, what you want to avoid. You need to allocate a buffer and use the InputStream's read method to fill it.
Keep in mind that you have to know the amount of incoming bytes or read multiple times while processing the already received data.
Alternatively, you could use the read method to get an int, check whether the stream has ended, then cast that int to a byte and process one by one. But usually, you want to use a buffer, since that's faster.
Convert InputStream to byte array in Java
You can use Apache Commons IO to handle this and similar tasks.
The IOUtils
type has a static method to read an InputStream
and return a byte[]
.
InputStream is;
byte[] bytes = IOUtils.toByteArray(is);
Internally this creates a ByteArrayOutputStream
and copies the bytes to the output, then calls toByteArray()
. It handles large files by copying the bytes in blocks of 4KiB.
Image Uri to bytesarray
From Uri
to get byte[]
I do the following things,
InputStream iStream = getContentResolver().openInputStream(uri);
byte[] inputData = getBytes(iStream);
and the getBytes(InputStream)
method is:
public byte[] getBytes(InputStream inputStream) throws IOException {
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
byteBuffer.write(buffer, 0, len);
}
return byteBuffer.toByteArray();
}
Getting bytes[] from inputstream in android gives null value
As pskink suggested in a comment, the issue was that the input stream has been read to the EOF by decodeStream, so there is nothing more to read.
To solve the problem, I created a temporary variable for inputstream.
Related Topics
Android:How to Change Playback Rate of Music Using Opensl Es
Android: How to Measure Total Height of Listview
How to Create Multiple Pendingintents with the Same Requestcode and Different Extras
Android Sax Parser Not Getting Full Text from Between Tags
Start Google Hangout from Intent in New Hangouts Android App
Does Retrofit Make Network Calls on Main Thread
Communicating with Serial Usb Device Over Android
Android: Broadcastreceiver Intent to Detect Camera Photo Taken
Stop Fragment Refresh in Bottom Nav Using Navhost
Taking a "Screenshot" of a Specific Layout in Android
How to Get Chrome Browsing History/Bookmarks in Our Android App
Firebase Authentication Not Working in Signed APK