I am developing an Android app that uploads image (jpg) to AWS S3. The image is being uploaded but I am unable to open it using web browser: the image is either invalid or opened as a string.
//skipping error handling for simplicity
InputStream is = Utils.streamFromUri(this, uri);
byte[] buffer = new byte[FileUtils.getInputSize(is)];
...
//is is restored at this point
is.read(buffer);
String data = new String(buffer);
S3.createObjectForBucket(bucketName, objectName, data)
Can you please share a working code for upload or give some directions on how to resolve this?
Thanks
First file is just 0 filled. Second file is not a jpeg but appears to be a result of some kind of transformation (there is some photo data in it). There should be no issue with upload under normal circumstances. If you provide specifics on how you uploaded them (code or tool) we can go from there.
Update
Your code to get data for upload is probably not going to work. I don’t know specifics about your utilities but when you do
it probably reads the entire stream, so you end up at the end of it and read nothing for upload. I highly recommend that you use Apache Commons IO to read data. And you can get size of it after it’s read into memory.
Update 2
You are using byte to string conversion. From
String(byte[])documentationthe platform’s default charset.
Since you use binary data, it’s getting transformed. That actually also can be seen from your second file which start with
ef bf bdwhich is Unicode replacement character. So refrain from using string for storing binary data.