如何上传gif文件以在android中解析为parsefile

问题描述 投票:3回答:1

我可以使用以下代码上传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文件,或者有更好的方法。谢谢

android image parse-platform bitmap gif
1个回答
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) {
                }
© www.soinside.com 2019 - 2024. All rights reserved.