2014-06-06 44 views
6

Tôi đang xem Parse Android docs và thấy rằng để lưu ảnh và video, bạn phải khởi tạo new ParseFile với tên và byte [] dữ liệu và lưu nó.Lưu và chụp lại ảnh và video trong Parse (Android)

Cách dễ nhất để chuyển đổi hình ảnh Uri và video Uri thành mảng byte là gì?

Dưới đây là giải pháp cố gắng của tôi:

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

Các convertImageToBytesconvertVideoToBytes phương pháp làm việc cho bây giờ, nhưng tôi chỉ tự hỏi nếu tôi đang làm việc này một cách chính xác.

+0

Có một lý do tại sao bạn giải mã hình ảnh và sau đó nén nó một lần nữa? – harism

+0

@harism Tôi không thực sự chắc chắn - Tôi tìm thấy nó trên một câu trả lời tràn Stack Tôi nghĩ. Tôi thực sự có rất ít kinh nghiệm với điều này, nhưng nó có thể bởi vì bạn phải chuyển đổi đường dẫn hình ảnh thành một Bitmap. – lschlessinger

+0

Chỉ cần tự hỏi vì nó có thể rất tốt là trường hợp không có nhu cầu cho hình ảnh riêng biệt và các phương pháp đọc mảng byte video. – harism

Trả lời

7

Từ Uri để có được byte [] Tôi làm những điều sau đây,

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. 
+0

có khả năng giảm kích thước tệp video trong khi chuyển đổi tệp video thành mảng byte không? vui lòng trả lời :( –

Các vấn đề liên quan