Сохранение и восстановление фотографий и видео в Parse (Android)

Я смотрел наРазбор Android документов и увидел, что для сохранения фотографий и видео, вы должны инициализироватьnew ParseFile с именем и байтом [] данных и сохраните их.

Какой самый простой способ преобразовать изображение Uri и видео Uri в байтовый массив?

Вот мои попытки решения:

mPhoto = new ParseFile("img", convertImageToBytes(Uri.parse(mPhotoUri)));
mVideo = new ParseFile ("vid", convertVideoToBytes(Uri.parse(mVideoUri)));

private byte[] convertImageToBytes(Uri uri){
    byte[] data = null;
    try {
        ContentResolver cr = getBaseContext().getContentResolver();
        InputStream inputStream = cr.openInputStream(uri);
        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        data = baos.toByteArray();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return data;
}

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

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

        videoBytes = baos.toByteArray();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return videoBytes;
}

private String getRealPathFromURI(Context context, Uri contentUri) {
    Cursor cursor = null;
    try {
        String[] proj = { MediaStore.Video.Media.DATA };
        cursor = context.getContentResolver().query(contentUri, proj, null,
                null, null);
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}    

convertImageToBytes а такжеconvertVideoToBytes методы работают на данный момент, но мне просто интересно, правильно ли я делаю это.

Ответы на вопрос(1)

Ваш ответ на вопрос