I am able to upload jpeg images and png images using the following code . However when I try to upload gif images with the same code, it gets saved as static file and shows up no animation . I am using glide to display gifs and it shows up with a animated gif url . Here's the code used to upload an image
public void createPost(String content, boolean imageAttached, Bitmap bitmap) {
if (DuzooActivity.isNetworkAvailable()) {
if (dialog != null) {
dialog.show();
dialog.setCancelable(false);
}
final ParseObject post = new ParseObject("Post");
post.put(DuzooConstants.PARSE_POST_CONTENT, content);
post.put(DuzooConstants.PARSE_POST_USER_NAME,
DuzooPreferenceManager.getKey(DuzooConstants.KEY_USER_NAME));
post.put(DuzooConstants.PARSE_POST_USER_IMAGE,
DuzooPreferenceManager.getKey(DuzooConstants.KEY_USER_IMAGE));
post.put(DuzooConstants.PARSE_POST_UPVOTES, 0);
post.put(DuzooConstants.PARSE_POST_DOWNVOTES, 0);
post.put(DuzooConstants.PARSE_POST_IS_FLAGGED, false);
post.put(DuzooConstants.PARSE_POST_FACEBOOK_ID,
DuzooPreferenceManager.getKey(DuzooConstants.KEY_FACEBOOK_ID));
post.put(DuzooConstants.PARSE_POST_INTEREST_TYPE,
DuzooPreferenceManager.getIntKey(DuzooConstants.KEY_INTEREST_TYPE));
post.put(DuzooConstants.PARSE_POST_TIMESTAMP, System.currentTimeMillis());
post.put(DuzooConstants.PARSE_POST_COMMENT_COUNT, 0);
post.put(DuzooConstants.PARSE_POST_HAS_MEDIA, imageAttached);
post.put(DuzooConstants.PARSE_POST_FAVORITE, false);
post.put(DuzooConstants.PARSE_POST_MY_VOTE, 0);
post.put(DuzooConstants.PARSE_POST_DELETED,false);
if (imageAttached) {
String name = path.substring(path.lastIndexOf("/"));
image = new ParseFile(name, Util.convertBitmapToBytes(bitmap));
image.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e == null) {
post.put(DuzooConstants.PARSE_POST_IMAGE, image);
post.pinInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if (dialog.isShowing())
dialog.dismiss();
}
});
post.saveInBackground();
}
}
});
} else {
post.pinInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if (dialog.isShowing())
dialog.dismiss();
returnToHomeActivity();
}
});
post.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
}
});
}
} else
Toast.makeText(this, "Sorry, no internet connection available", Toast.LENGTH_SHORT).show();
}
Here is the convertToBytes function.
public static byte[] convertBitmapToBytes(Bitmap bitmap) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, baos);
byte[] b = baos.toByteArray();
return b;
}
Is there a way I can upload gif files with any tweaks in the above code or is there a better way . Thanks
I got the answer to the above problem`
File file = new File(path);
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
int read;
byte[] buff = new byte[1024];
while ((read = in.read(buff)) > 0) {
out.write(buff, 0, read);
}
out.flush();
byte[] bytes = out.toByteArray();
image = new ParseFile(name, bytes);
image.saveInBackground(new SaveCallback() {
public void done(ParseException e) {
// Handle success or failure here ...
}
}, new ProgressCallback() {
public void done(Integer percentDone) {
// Update your progress spinner here. percent done will be between 0 and 100.
}
});
} catch (FileNotFoundException ex) {
} catch (IOException ex) {
}