Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJason Losito
Courses Plus Student 8,865 PointsImage file uploading using OKHTTP
Hi,
Can someone help me on how to upload an image file using OKHTTTP?
I am new to android development and I am creating an inventory where there will be images for products in the inventory. I am able to using OKHTTP for sending data to MySQL using PHP and JSON. However, I can't find any tutorials or samples on how to upload images. Even the OKHTTP docs are not clear form (Or perhaps I am really a beginner and do not understand what the docs mean).
Please help me.
Thanks.
1 Answer
Ben Junya
12,365 PointsYo!
Glad you're using OkHttp. AsyncTask sucks. At my current and last workplace, we talked about how we disliked AsyncTask with burning passion.
I found this bunch of OkHttp recipes for you to check out. The one you're looking for is "posting a file":
https://github.com/square/okhttp/wiki/Recipes
You're going to need to start using the Android Filesystem in order to locate the file too.
Read up on these API's http://developer.android.com/reference/java/io/File.html http://developer.android.com/reference/android/os/FileObserver.html <-- Important if you're using the camera!
I have an example I wrote for a recent job interview you can check out here. It was a code challenge for a company here in Los Angeles. Check out MainActivity.java https://github.com/MrBenJ/WritingFilesAndroid/blob/master/app/src/main/java/com/prismmobile/cyberdustcc/MainActivity.java
Look at this particular block of code to see how files are retrieved from the system:
FileInputStream fileInputStream;
try {
fileInputStream = openFileInput("list");
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
ArrayList<String> noteList = (ArrayList<String>) objectInputStream.readObject();
objectInputStream.close();
}
catch(Exception e) {
e.printStackTrace();
}
Here's a block of code that writes to a file
@Override
public void onPause() {
super.onPause();
try {
FileOutputStream fileOutputStream = this.openFileOutput("list", Context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fileOutputStream);
oos.writeObject(noteList);
oos.close();
}
catch(Exception e) {
e.printStackTrace();
}
I know this isn't particularly the answer you're looking for, but hopefully it sheds some insight on what you're trying to do.
Good luck!
Jason Losito
Courses Plus Student 8,865 PointsJason Losito
Courses Plus Student 8,865 PointsHi Ben,
Thanks for your time.
I was able to figure it out
I just needed to modify the examples provided by okhttp. Here is the part that I have changed:
RequestBody requestBody = new MultipartBuilder() .type(MultipartBuilder.FORM) .addFormDataPart("image", mReceivingReportItemImageFileName, RequestBody.create(MediaType.parse("image/jpeg"), new File(mMediaUri.getPath()))) .build();