Jones
Jones

Reputation: 33

Saving an image array to a file

I have an array that looks like

String image = obj.getString("data");

[255,216,255,224,0,16,74,70,73,70,0,1,1,0,0,1,0,1,0,0, ....]// this is what image contains.

I try to convert it to a byte array by

byte[] bytes = image.getBytes();

Then I tried saving it with

File file = new File(Environment.getExternalStorageDirectory(), "DirImage/test.jpg");
FileOutputStream fos = new FileOutputStream(file);
fos.write(bytes);
fos.flush();
fos.close();

The file gets written but I cannot se the image.shows me a broken file.

Upvotes: 0

Views: 234

Answers (2)

greenapps
greenapps

Reputation: 11224

First determine the number of bytes you need. So the amount of decimal values contained in the string. Then declare a byte array like byte bytes[] = new byte[amount];. You could also split that string in a string array first. String parts [] = image.split(",");. parts.length would tell 'amount'.

Then loop throught the parts and convert one by one the parts string to a byte.

Upvotes: 1

speXy
speXy

Reputation: 53

I never worked directly with JPEG before,but i think that you need to specify some info at the beginning of the file,instead of outright outputting raw color values,try googling for jpeg structure.

Upvotes: 0

Related Questions