我可以使用以下代码上传jpeg图像和png图像。但是,当我尝试使用相同的代码上传gif图像时,它将保存为静态文件并且不显示任何动画。我正在使用滑动来显示GIF,它会显示一个动画gif网址。这是用于上传图像的代码
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();
}
这是convertToBytes函数。
public static byte[] convertBitmapToBytes(Bitmap bitmap) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, baos);
byte[] b = baos.toByteArray();
return b;
}
有没有办法可以在上面的代码中上传任何调整的gif文件,或者有更好的方法。感谢
答案 0 :(得分:3)
我得到了上述问题的答案。
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) {
}