Reputation: 526
I am working on an app, which requires-
I basically know how to implement them individually. For example, I have already managed to pull local contacts, I am yet to achieve 2 and 3. I have few questions regarding them.
The questions may seem broad, but I feel that they are tightly coupled, considering a single app. I am expecting expert opinion on the recommended ways of achieving these features.
Thanks!
Upvotes: 0
Views: 128
Reputation: 3827
About third question, if you're going to keep using Volley
, you can try to override the getBody() to return the image's bytes, rest of other parameters should be encoding within the URL, this way would use both of GET and POST method.
public class ContactRequest extends StringRequest {
public static String buildRequestUrl(String url,
Map<String, String> params, String paramsEncoding) {
StringBuilder urlBud = new StringBuilder(url).append('?');
try {
for (Map.Entry<String, String> entry : params.entrySet()) {
urlBud.append(URLEncoder.encode(entry.getKey(), paramsEncoding));
urlBud.append('=');
urlBud.append(URLEncoder.encode(entry.getValue(), paramsEncoding));
urlBud.append('&');
}
return urlBud.toString();
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("Encoding not supported: " + paramsEncoding);
}
}
private String imageFilePath;
public ContactRequest(String url, String imageFilePath,
Response.Listener<String> listener, Response.ErrorListener errorListener) {
super(Method.POST, url, listener, errorListener);
this.imageFilePath = imageFilePath;
}
@Override
public byte[] getBody() throws AuthFailureError {
return getBytesFromFile(new File(imageFilePath));
}
}
build the ContactRequest
and serve to RequestQueue like this :
String originUrl = "http://.../contact_push.do";
String imageFilePath = "/sdcard/.../contact_avatar_path";
Map<String, String> params = new HashMap<String, String>();
params.put("firstName", "Vince");
params.put("lastName", "Styling");
new ContactRequest(
ContactRequest.buildRequestUrl(originUrl, params, HTTP.UTF_8),
imageFilePath, null, null);
'cause I never faced this problem before, so I don't sure this Request can reach to the server correctly, it's an un-test solution for me, hope can help.
Upvotes: 0
Reputation: 93668
You typically save them either to your internal folder or to the SD card in your directory. The internal data folder will be locked to your app (unless the phone is rooted) and inacessible by other apps, the sd card will be only on 4.3 and higher. Either way you should manage the amount of data cached, set a limit and not allow it to go higher than that (kicking them out in some matter, most likely LRU or LFU). YOu'll need to do that by hand or find a library to do it for you, its not built into Android.
As for downloading them from the server- typically its just an HTTP request, with a webservice that will do any necessary privacy checking before sending down either an image result or an error. You don't want to do anything like JSON or the like here, it will just waste bandwidth.
Upvotes: 1
Reputation: 6857
Volley
library is for general https interaction but not for images downloading, encoding and caching. Picasso is aimed to work with images: loads images from network or by Uri
, has cache size settings and many other features.Upvotes: 0