如何上传 gif 文件以在 android 中作为解析文件进行解析
How to Upload gif files to parse in android as a parsefile
我可以使用以下代码上传 jpeg 图像和 png 图像。但是,当我尝试使用相同的代码上传 gif 图像时,它被保存为静态文件并且没有显示动画。我正在使用 glide 来显示 gif,它显示了一个动画 gif url 。这是用于上传图片的代码
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 文件,或者有没有更好的方法。谢谢
我得到了上述问题的答案`
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) {
}
我可以使用以下代码上传 jpeg 图像和 png 图像。但是,当我尝试使用相同的代码上传 gif 图像时,它被保存为静态文件并且没有显示动画。我正在使用 glide 来显示 gif,它显示了一个动画 gif url 。这是用于上传图片的代码
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 文件,或者有没有更好的方法。谢谢
我得到了上述问题的答案`
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) {
}