user3278732
user3278732

Reputation: 1724

Android string file to image

from server side an image is being sent to me. The image i receive it as string, what server side is sending me is the entire file. How can I read it in android side?

File tempFile = File.createTempFile("image", ".jpg", null); 
// tempFile.wr
byte[] bytes = output.toString().getBytes();
FileOutputStream fos = new FileOutputStream(tempFile);
fos.write(bytes);

output is the result i receive, its string which is the entire file send from server side.

Bitmap myBitmap = BitmapFactory.decodeFile(tempFile.getAbsolutePath());
m2.setImageBitmap(myBitmap);

I am getting SkImageDecoder::Factory returned null

this code running produces this log cat

File tempFile = File.createTempFile("image", ".jpg", null);
FileOutputStream fos = new FileOutputStream(tempFile);
fos.write(output.toString().getBytes());
fos.close();  
System.out.println(""+tempFile.getAbsolutePath());
BitmapFactory.Options myOptions = new BitmapFactory.Options();
myOptions.inDither = true;
myOptions.inScaled = false;
myOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// important
myOptions.inPurgeable = true;
Bitmap bitmap  = BitmapFactory.decodeFile(tempFile.getAbsolutePath());
m2.setImageBitmap(bitmap);

http://www.speedyshare.com/8BdVs/log.txt

Upvotes: 0

Views: 95

Answers (1)

Arpit Patel
Arpit Patel

Reputation: 1571

Here is the code to convert into stream from base64 decoded string of image. This will only work if it has properly encoded in Base64 and stored it in your server

 Bitmap img = null;
 InputStream stream = new ByteArrayInputStream(Base64.decode(imageDataBytes.getBytes(), Base64.DEFAULT));
 img = BitmapFactory.decodeStream(stream);

Upvotes: 1

Related Questions