-2

I need to get the byte array from file path to upload the image. But the byte array in the form of array.How do i get the byte array. I have followed the following steps but could not found the solutions.

I have tried the following code but does not work.

byte []buffer=new byte[1024];
    ByteArrayOutputStream os=new ByteArrayOutputStream();
    FileInputStream fis=new FileInputStream(f);
    int read;
    while ((read=fis.read(buffer))!=-1){
        os.write(buffer,0,read);
    }
    fis.close();
    os.close();

It return the byte array object but i need the array. When i used Array.toString(bytearray) it return in the string form but i need the array form. Please help me how can i do this.

1 Answers1

1

convert the bytes to file, see below code.

InputStream is = Context.openFileInput(someFileName);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];

while ((int bytesRead = is.read(b)) != -1) { 
    bos.write(b, 0, bytesRead);
}

byte[] bytes = bos.toByteArray();

or

byte[] fileContent = Files.readAllBytes(file.toPath());
Gaurav
  • 171
  • 1
  • 13
  • by using the above code i get the the byte array like this:- "byteArray": "[B@b0b6b8c", but i need in the the array. – Rishikesh Rahi Jun 26 '19 at 07:05
  • you want a int[] array? – Gaurav Jun 26 '19 at 07:06
  • @RishikeshRahi That's the result of calling `toString()` on the byte array, directly or indirectly, e.g. via `String.valueOf()` or `print()` or `println()`. Solution: don't. – user207421 Jun 26 '19 at 07:06
  • @gauravgajjar yes – Rishikesh Rahi Jun 26 '19 at 07:08
  • public int[] convert(byte buf[]) { int intArr[] = new int[buf.length / 4]; int offset = 0; for(int i = 0; i < intArr.length; i++) { intArr[i] = (buf[3 + offset] & 0xFF) | ((buf[2 + offset] & 0xFF) << 8) | ((buf[1 + offset] & 0xFF) << 16) | ((buf[0 + offset] & 0xFF) << 24); offset += 4; } return intArr; } – Gaurav Jun 26 '19 at 07:11
  • try this code to convert in to int array – Gaurav Jun 26 '19 at 07:11
  • @RishikeshRahi If you want an `int[]` array why do both your title and your question ask for a `byte[]` array? – user207421 Jun 26 '19 at 07:12
  • @gauravgajjar it still return like [I@d6028e7. – Rishikesh Rahi Jun 26 '19 at 07:28
  • you print a array in log? – Gaurav Jun 26 '19 at 07:29
  • yes i do that but it still like above – Rishikesh Rahi Jun 26 '19 at 07:31
  • i need like this but not in string it should be in array."byteArray": "[-109, -75, 90, -103, -101, 44, 118, 38, 112, -4, 35, -107, -123, -66, -69, -54, 106, -85, -40, 86, 20, 90, 42, -18, 54, -13, -45, 39, 42, -73, 59, -8, -86]" – Rishikesh Rahi Jun 26 '19 at 07:34
  • @RishikeshRahi (1) You haven't answered my question; (2) 'not in string' is meaningless. You've already been told why you're getting what you're getting. [`java.util.Arrays.toString()`](https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#toString-byte:A-) will produce exactly the output you want. But only if you *call* it. – user207421 Jun 26 '19 at 10:30