java - Android - 如何将动画 GIF 文件上传到 Parse Server

标签 java android gif parse-server parse-android-sdk

我正在开发一个具有 Parse Server Android SDK 的应用,并且我知道如何将视频文件转换为 bytes[] 数组,这就是我使用的代码:

private byte[] convertVideoToBytes(Uri uri){
        byte[] videoBytes = null;
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            FileInputStream fis = new FileInputStream(new File(getRealPathFromURI(uri)));

            byte[] buf = new byte[1024];
            int n;
            while (-1 != (n = fis.read(buf)))
                baos.write(buf, 0, n);

            videoBytes = baos.toByteArray();
        } catch (IOException e) { e.printStackTrace(); }
        return videoBytes;
    }
    // GET VIDEO PATH AS A STRING -------------------------------------
    public String getRealPathFromURI(Uri contentUri) {
        String[] filePathColumn = { MediaStore.Images.Media.DATA };
        Cursor cursor = ctx.getContentResolver().query(contentUri, filePathColumn, null, null, null);
        assert cursor != null;
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String filePath = cursor.getString(columnIndex);
        cursor.close();
        Log.i(TAG, "VIDEO PATH: " + filePath);
        return filePath;
    }


但是我无法上传GIF文件,我已经尝试过这段代码,但它没有做任何事情,我的数据库中的.gif文件的单元格在保存后为空在后台使用saveInBackground():

Uri path = Uri.parse("android.resource://" + BuildConfig.APPLICATION_ID + "/" + R.drawable.my_animated_gif_from_drawable);
String gifPath = path.toString();

File file = new File(gifPath);
Log.i(TAG, "GIF PATH: " + file.getAbsolutePath());

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();

    ParseFile gifFile = new ParseFile(gifName + ".gif", bytes);
    bObj.put(BUZZ_GIF, gifFile);

} catch (FileNotFoundException ignored) {
} catch (IOException ignored) { }


有谁知道如何将 .gif 文件作为 ParseFile 上传,或者只是将其转换为 bytes[] 以便我可以使用 新的 ParseFile 函数?

最佳答案

这里是上传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);
                } catch (FileNotFoundException ex) {
                } catch (IOException ex) {
                }


 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) {
                }
            });
        }
    } 

关于java - Android - 如何将动画 GIF 文件上传到 Parse Server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55166870/

相关文章:

java - android应用程序中的xml解析文件存储在哪里

android - 为什么 Android Studio 不会在 Release模式下创建我的 AAR 文件

android - 如何在 Android 中限制 Spinner 下拉 View 的高度

java - 为什么我的 javafx imageview 扭曲了我的 gif 的播放方式

ffmpeg - 强制 FFMPEG 重用 gif 帧

java - 无数字 Java 正则表达式模式

java - 在内部类中绕过 "final or effectively final"会给出非常随机的结果

java - 如何设置过滤器链执行顺序

java - AAR 库似乎无法在 Android Studio 0.5.8 中工作。我究竟做错了什么?

java - 在 JComboBox 中使用动画 GIF