在 Parse 中保存和恢复照片和视频 (Android) [英] Saving and retreving photos and videos in Parse (Android)

查看:21
本文介绍了在 Parse 中保存和恢复照片和视频 (Android)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看 Parse Android docs 并看到它可以保存照片和视频,你必须用一个名字和一个字节[]的数据初始化一个new ParseFile并保存它.

I was looking at the Parse Android docs and saw that to save photos and videos, you have to initialize a new ParseFile with a name and a byte[] of data and save it.

将图像 Uri 和视频 Uri 转换为字节数组的最简单方法是什么?

以下是我尝试的解决方案:

Here are my attempted solutions:

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

convertImageToBytesconvertVideoToBytes 方法现在可以使用,但我只是想知道我是否正确执行此操作.

The convertImageToBytes and convertVideoToBytes methods work for now, but I'm just wondering If I'm doing doing this correctly.

推荐答案

从Uri到get byte[]我做了以下几件事,

From Uri to get byte[] I do the following things,

 ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileInputStream fis = new FileInputStream(new File(yourUri));

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

byte[] videoBytes = baos.toByteArray(); //this is the video in bytes.

这篇关于在 Parse 中保存和恢复照片和视频 (Android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆