0

I'm working on an app that has Parse Server Android SDK, and I know how to convert a video file into a bytes[] array, this is the code I use:

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


But I'm unable to upload a GIF file, I've tried this code, but it doesn't do anything, the cell of my .gif file in my database is empty after saving in background with 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) { }


Does anyone know how to upload a .gif file as a ParseFile, or maybe just to convert it into bytes[] so that I can use the new ParseFile function?

Tom Fox
  • 897
  • 3
  • 14
  • 34
Frank Eno
  • 2,581
  • 2
  • 31
  • 54

1 Answers1

1

Here is the way to upload gif file to parse server

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) {
                }
            });
        }
    } 
Ankit Tale
  • 1,924
  • 4
  • 17
  • 30