Log.v(“ThreeTips”, “#6”)

Mert SIMSEK
1 min readNov 10, 2016

--

1- Convert Uri to File in Android

Most of image picker libraries give you a callback and Uri of selected media. But converting them into a file is not always that easy because of different android versions. Talk is cheap. Here is the solution which I copied from SO. And how we call it.

File myFile = FileUtil.getPickedPictureFile(context, filePath)

2- Retrofit 2.0 : Upload image file to server

Create a retrofit interface method which takes image as param.

@Multipart
@POST("upload/image")
Observable<UpResponse> uploadImage(@Part MultipartBody.Part image);

Then upload image file.

RequestBody reqFile = RequestBody.create(MediaType.parse("image/*"), file);
MultipartBody.Part body = MultipartBody.Part.createFormData("image", file.getName(), reqFile);
retrofitInterface.uploadImage(body);

3- You cant change quality of PNG

Maybe this is a well-known thing but I just experienced that. You can not compress PNG image. After I spent 2 hours, I found a SO post that tells me PNG can not be compressed, you should use .JPEG format If you want to compress you image.

Here is code snippet to compresses bitmap and returns file.

Enjoy your coding. If you like this series please recommend it to make it reachable. By the way I want to thank to Orhan to let me steal his blog series title.

--

--

Mert SIMSEK
Mert SIMSEK

No responses yet