File uploading Not getting regular callback response? - android

I am trying to upload a 20mb video file to cloud using http put method. File is uploading as byte array format. To show the progress of upload, I add progress callback with entity. But now I'm getting the call back response after uploading the full file and response show 100.00.
How do I get the callback after in every 10kb uploading?
Here is my code:
byte[] videoBytes = ous.toByteArray();
httput.setEntity(new ByteArrayEntity(videoBytes));
httput.setEntity(new ProgressHttpEntityWrapper(httput.getEntity(), progressCallback));

Related

How to decode a very large image on android?

I am decoding an image(ARGB) that is 8000x8000 pixels, so in uncompressed form it reaches
(2 + 2 + 2 + 2) * (8000 * 8000) = 488 MB
and crashes the android. I don't want to sample the image because I am converting the bitmap to byte array and sending it in a PUT request. I have tried "decodeRegion" but I don't know how to stitch the data(i.e. byte arrays) back together , since they have the head info at start and just concatenating them isn't helping.
Use an HTTP client library that allows you to upload from a file or stream, so that you do not need to decode the image and try to hold it in memory. OkHttp has options for this; see this recipe for streaming a POST request, this recipe for POSTing a file, or this recipe for multipart POSTs. Those techniques should be adaptable to a PUT request.
Why are you reading in a large image, decoding it, then posting the byte array? That's the wrong way to do it.
If your API actually requires the decoded bytes, fix it. More likely it wants the file's raw data. In which case you just need to use any networking API that gives you an OutputStream, and read in the file's data 1 MB at a time, reading it from the File's InputStream and writing it to the socket's OutputStream

Retrofit 2 RequestBody Content Length Greater Than File Size

I have an Api which requires me to send up the size of a file in bytes before I then send up the actual file. When I call file.length() on the file to send up in the first Api call, it returns 1996.
When I then package the file up into a RequestBody the contentLength() becomes 2556. The server then rejects this, saying that these sizes must match.
Here is the code for creating the RequestBody.
val requestBody = RequestBody.create(
MediaType.parse("image/jpeg"),
avatarFile)
What is being added in the RequestBody to increase its content length?
Should I just send up the size of the RequestBody on the first call to get around this problem?
EDIT
Here is where I call the API the first time to send the size initially:
return authService.updateAvatar(
AvatarMetadataRequest(
size = avatarFile.length().toInt(),
crc = profileImageProvider.getFile()!!.checksum()!!.toInt()))
And then this is where I call it the second time when the file actually gets uploaded:
val avatarFile = profileImageProvider.getFile()
val requestBody = RequestBody.create(
MediaType.parse("image/jpeg"),
avatarFile)
return authService.uploadAvatar(
id.split("/").last(),
MultipartBody.Part.createFormData("avatar",
profileImageProvider.getFileName(),
requestBody))
It turns out the contentLength() of the requestBody is correct. So the Content-Length must be being modified or increased when the MultipartBody.Part.createFormData is called.
This seems to be a mistake on the part of the avatar upload API. The avatar is being uploaded as MultipartData and so will never equal the raw size of the file. This is because a multipart request will have additional data besides the file.
Here is an example multipart request:
--------------------------0b880724ca8aacd6
Content-Disposition: form-data; name="file1"; filename="test.txt"
Content-Type: text/plain
test test test
--------------------------0b880724ca8aacd6--
As you can see, the content of the file is just test test test. Everything else is additional data added by the multipart request.
The raw size on disk is 15 bytes, while the Content-Length of the request is 202, due to all the extra data.
So the avatar upload API should not just be checking the Content-Length of the entire request, but rather extracting the specific part relating to the file and just checking the size of the data contained in that part.
If you have no control over the behaviour of the avatar upload API, then you can attempt to calculate the total Content-Length by figuring out the total size of the overhead added by the multipart request and adding that to your file size.

Retrieve Android/Cloudinary uploaded image URL

Im using Cloudinary along with an Android App.
Im uploading a image using:
cloudinary.uploader().upload(imagePath, ObjectUtils.emptyMap());
The problem is that I need the url to retrieve this image later. Is there any method that I can use to store the uploaded image url inside my App?
Thanks!
According to their documentation
Documentaion
This code shows
Map uploadResult = cloudinary.uploader().upload(file, ObjectUtils.emptyMap());
Uploading is performed synchronously. Once finished, the uploaded image is immediately available for manipulation and delivery.
An upload API call returns a JSON object with content similar to that shown in the following example:
{
"public_id":"tquyfignx5bxcbsupr6a",
"version":1375302801,
"signature":"52ecf23eeb987b3b5a72fa4ade51b1c7a1426a97",
"width":1920,
"height":1200,
"format":"jpg",
"resource_type":"image",
"created_at":"2013-07-31T20:33:21Z",
"bytes":737633,
"type":"upload",
"url":
"http://res.cloudinary.com/demo/image/upload/v1375302801/tquyfignx5bxcbsupr6a.jpg",
"secure_url":
"https://res.cloudinary.com/demo/image/upload/v1375302801/tquyfignx5bxcbsupr6a.jpg",
"etag":"1adf8d2ad3954f6270d69860cb126b24"
}
The response is automatically parsed and converted into a Map.

How to parse the response of youtube video upload api to get the video's url

I'm using the youtube api's non resume method to upload a video to youtube from my android app. The response of this upload is an xml file. I need to parse it and find the url of the video (WHICH IS IN THE TAG media:player url). How to get that url?
The xml is as follows
Finally I decided to parse that XML using the string manipulation methods. That thing was a success as I need only the url. I manipulated the string as follows
int start = youtubeXml.indexOf("<media:player url='");
String urlPart = youtubeXml.substring(start);
youtubeUrl = (String) urlPart.subSequence(urlPart.indexOf("http"), urlPart.indexOf(";feature"));
Here youtubeXml is a String which contains the String representation of the output data stream
The video ID is under:
http://gdata.youtube.com/feeds/api/videos/[id is here]
You can start an android VIEW activity for the URL: "http://youtube.com/[video id]" which will launch the default YT player with the video (or ask the user to chose a player from a list).

Android: get image from base64binary format

I use web service to get image. The service response contains image in base64Binary format. I try to decode response data with Base64.decode() (http://iharder.sourceforge.net/current/java/base64/). See my code below:
byte[] data = Base64.decode(responseString);
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
imageView.setImageBitmap(bmp);
decodeByteArray always return null.
I try to save data in .png file. I can open this file on my PC and in the Android File Manager application. But preview activity of File Manager couldn't open this file.
Then i try to parse this data using .NET client with Convert.Base64() method. And this image have been processing successfully. Then i compare byte array in image created with android client and .NET client. The differences were in sign of bytes. .NET uses unsigned bytes but Java use only signed bytes. Is this is a reason of my problem?
Is anybody have the same problem in decoding of base64Binary?
Here is one solution, and for me is working (knowing that the format in which the image comes from the server through the web service is base64binary)
decodedIcon[] = null;
byte[] bb = (resposeString).getBytes("utf-8");
decodedIcon = Base64.decodeBase64(bb);
Bitmap bitmap = BitmapFactory.decodeByteArray(decodedIcon, 0,
decodedIcon.length);
//then you get the image view and setImageBitmap(bitmap)
PS:
Base64.decodeBase64 comes from the library org.apache.commons.codec.binary.Base64;
You should have commons-codec-1.3.jar included in the assets folder
the version doesn't have to be 1.3
Thanks to one of my friends for this hint.

Categories

Resources