Getting a .png image by Apache HttpClient on Android - java

I'm trying to get a .png file to render in a WebView after getting the file through an apache HttpClient get request. I'm actually reading the HttpResponse contents into a simple byte array -> (evaluating the contents into a String when debugging).
It appears if I convert those bytes into a string to compare with the actual file, they look different. Is there a specific way to retrieve .png file
using the apache HttpClient?
example compare, my test is on the left and actual file on right

Related

Get real path from URI? Android 9+

I have the uri of my files. Now I need to get its real path to send them. (the uri is like: "content://some-path").
This is for Android 9
So with Android 9+ you can only use file paths inside you applications private storage area otherwise you have to use URI's and ContentResolvers
See https://developer.android.com/training/data-storage/files/external-scoped for details of the changes in 9+
So I see three solutions when using Retrofit2
Get a Java FileDescriptor from a contentResolver and read the file from the contentResolver, writing it to your App's private storage area. You can then get a Java File Object as normal from this copy of the file. This is similar to stackoverflow.com/a/52814247/3518278 as suggested by ViVekH
Get a Java FileDescriptor from a contentResolver and read it in to a in memory Byte array. I'm not a Retrofit2 user but I believe you can create Request Body or Multipart part from a Byte array instead of a Java File object.
Raise a feature request with RetroFit2 / OKHTTP to be able to give it a Java FileDescriptor object instead of a File Object
Note with the contentResolver you can query it to get the "Display Name" as the filename part of the Path.

How to Write Image File from Request Body Without Writing to Memory Nodejs

I'm trying to (HTTP) POST an image to a Nodejs server that is configured using Express. I have been able to accomplish this successfully using JSON, but unless I am mistaken, there is no way to obtain the image string without loading the entire request body into a new variable before parsing it as JSON. Since images are quite large and the image should already be stored in the request body anyway, is there a way to immediately pipe the image contents into fs.writeFile()? The content type for the request does not have to be JSON. I have tried using a querystring as well, but that was unsuccessful. The content type cannot be just an image though because I have to include a tag for the image too (in this case the user's email address).
Here is the code for when I attempted to use a query string. It is located in a post route method for the express app:
fs.writeFile('profiles/images/user.png', new Buffer(req.body.image, 'base64'),
function(error)
{
if (error)
res.end(error);
}
);
No error occurs, and the code creates the .png file, but the file is somehow corrupted and is larger than it should be.
All of this is actually for an Android app, so here is also the Java code that I am using to send the request:
URLConnection connection = new URL(UPLOAD_PICTURE_URL).openConnection();
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;");
connection.setDoOutput(true);
String image = Base64.encodeToString(
IOUtils.toByteArray(new FileInputStream(filePath)),
Base64.NO_WRAP
);
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write("email=" + email + "&image=" + image);
out.close();
Perhaps this belongs in another topic, but along the same lines, does anybody know a way to pipe the file input stream in the android code directly to the URLConnection's output stream with base64 encoding? I have tried writing the string literal (the out.write() line above ^) and then creating a Base64OutputStream to write the image before piping that stream into the URLConnection's outputstream, but calling req.body.image in the node app after doing that just returns undefined. And finally, does anybody know if IOUtils.toByteArray() (from Apache Commons), when used as the input argument for an input/output stream constructor, writes the entire byte array to memory anyway on the Android side? If so, is there a way of avoiding that too?
Thanks in advance.

write byte array to csv file with java

I have to upload a file attachment and send it via an HTTP GET request to a server location. I am using IBM websphere integration designer to implement this functionality.
I have the attachment as a byte array and it has to be converted to an Excel .csv file.
Please help me understand how to write a .csv file using a byte array.
If you have a byte array. Let's say.
byte[] byteArray = ThisIsWhereMyByteArrayIsComingFrom.getArray();
String convertedString = new String(byteArray);
That gets the string value of whatever your byte array is. Then you can either, use a csv library to output whatever it is to a file.

Convert inputstream to pdf to byte array

i created a mule flow which accepts a file as inputstream and convert into byte array and attach that file to the particular SalesForce case.
Can it possible that i could convert the file(coming as inputstream) to the pdf format and attach to Salesforce case.
PDF is a special format so directly it will not create PDF. You can use libraries like Apache FOP or iText to create PDF output.

The Process to Store & Retrieve an Image in a Database

I have never saved and retrieved an image to and from the database before. I wrote down what I guessed would be the process. I would just like to know if this is correct though:
Save image:
Select & Upload image file from jsp (Struts 2) which will save it as a .tmp file.
Convert the .tmp file to a byte[] array (Java Server-Side)
Store the byte[] array as a blob in the database (Java Server-Side)
Get image:
Get the byte[] array from the database (Java Server-Side)
Convert the byte[] array to an image file (Java Server-Side)
Create the file in a location (Java Server-Side)
Use an img tag to display the file (JSP Client-Side)
Delete the file after it's finished being used? (Java Server-Side)
I'm aware of the fact that it is highly recommended to not save & retrieve images to and from the database. I would like to know how to do it anyway.
Thanks
Almost correct.
It's expensive and not so great to create the file on the fly and then delete it.
Yes, you store it as the raw bytes in the database, but the way to retrieve it and display it to a client machine is to implement a web handler that sets the content-type of the response to the appropriate MIME type and then dumps the bytes out to the response stream.
Yes, You get it right.
Save Image :
The decision to save image is very much dependent on further usage. You have one option to save the file on the file system. The location for saved file should be saved into the metadata in the database table.
Get Image:
You do not have to right file data on any temp location. It can be easily rendered from the database only. Just send a request from client and intercept that request in a spacial designed Servlet. This Servlet will read the file metadata and corresponding file, if successful, write the file back on the response stream.

Categories