Upload Image to Imgur Anonymously Using Volley in Phimpme Android app
As Phimpme Android is an image app in which lets you share your image on multiple platform without installing that apps like Twitter, Facebook, Pinterest and Imgur. Imgur is the best place to share and enjoy the most awesome images on the Internet. Imgur provides APIs to upload image from your account as well as anonymously. In this blog I am going to explain how I added the imgur upload feature in Phimpme Android app.
I have implemented upload to Imgur anonymously. There is step by step guide to implementing it.
Step 1: Register your application on Imgur.
To register your application click here, For more detail read their documentation here Imgur API site.
Once you have registered your application, you will be provided with two keys, Client ID and Client Secret. Save them, we will need them later to upload an image and for = authentication purposes.
Step-2: Add a networking library in your Gradle, I have used volley.
Olley is a good networking library, works well for both API requests and File upload. To add volley in the project, add the following dependency
'com.android.volley:volley:1.0.0'
Step-3 Convert your image into the desired format to upload.
Imgur three kinds of images: A binary file, base64 data, or URL of an image. (Up to 10MB).
In phimpme Android, we are uploading base64 image string.
So, we have to first convert our image to the bitmap and then convert the bitmap to base64. I recommend using an Image library to decode bitmap otherwise, there are chances of OutOfMemory exception thrown for large image files.
To convert bitmap to base64 string
public static String get64BaseImage (Bitmap bmp) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos); byte[] imageBytes = baos.toByteArray(); return Base64.encodeToString(imageBytes, Base64.DEFAULT); }
Best practice to add such methods into theutility class. Also, you can apply the image compression in above if you want to reduce the image size, here number 100 represents, preserve the 100% quality of the image.
Step-4: Now we need 2 things, add image string in body and add AUTHENTICATION key in headers of the request.
We have added two methods to do above mentioned tasks.
- To upload the image data to Imgur.
- To add a header in our network request.
The header is required for Imgur to authenticate the client who uploads the file and your authentication key is your CLIENT-ID, which we have generated in step 1.
@Override protected Map<String, String> getParams() throws AuthFailureError { Map<String, String> parameters = new HashMap<String, String>(); parameters.put("image", imageString); if(caption!=null && !caption.isEmpty()) parameters.put("title",caption); return parameters; }
Now add the headers to authenticate the client.The above code contains the body part with the key as “image” and value as the imageString data, which was the result of get64BaseImage () method.
@Override public Map<String, String> getHeaders() throws AuthFailureError { Map<String, String> headers = new HashMap<String, String>(); headers.put("Authorization",”Client-Id {client-id}); return headers; }
Override the getHeader method of Volley library and return a map which has a key named “Authorization” and value is client id of Imgur.
Now we are ready to upload an image on Imgur through Phimpme Android app.
The problem I faced:
Whenever I was trying to upload large size images, I was getting volleytimeout exception, by default connection timeout was not sufficient to upload large files, so I resolved this error by adding below line in the request policy.
request.setRetryPolicy(new DefaultRetryPolicy( 50000, 5, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
Now it works seamlessly with large files even on slow internet network and we are receiving the URL of the image in the response.
Resources :
- Official Imgur documentation : https://apidocs.imgur.com/
- Upload Image documentation : https://apidocs.imgur.com/#4b8da0b3-3e73-13f0-d60b-2ff715e8394f
- Imgur API example on stackoverflow : https://stackoverflow.com/questions/17352866/imgur-api-uploading
- Relevant code on Phimpme repository : https://github.com/fossasia/phimpme-android/blob/development/app/src/main/java/org/fossasia/phimpme/SharingActivity.java#L445
You must be logged in to post a comment.