2014-06-06 14 views
6

Ich habe mir die Parse Android docs angesehen und gesehen, dass Sie zum Speichern von Fotos und Videos einen new ParseFile mit einem Namen und einem Byte [] Daten initialisieren und speichern müssen.Fotos und Videos in Parse (Android) speichern und wiederherstellen

Was ist der einfachste Weg, um ein Bild Uri und Video Uri zu einem Byte-Array zu konvertieren?

Hier sind meine versuchten Lösungen:

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

Die convertImageToBytes und convertVideoToBytes Methoden für jetzt arbeiten, aber ich frage mich nur, wenn ich tue dies richtig tun.

+0

Gibt es einen Grund, warum Sie Bild entschlüsseln und dann wieder komprimiert werden? – harism

+0

@harism Ich bin mir nicht wirklich sicher - ich fand es auf einer Stack Overflow Antwort, denke ich. Ich habe wirklich sehr wenig Erfahrung damit, aber es könnte sein, weil Sie den Bildpfad in eine Bitmap konvertieren müssen. – lschlessinger

+0

Nur wundernd, weil es sehr gut der Fall sein kann, gibt es keine Notwendigkeit für separate Bild-und Video-Byte-Array-Lese-Methoden. – harism

Antwort

7

Von Uri byte [] ich die folgenden Dinge zu bekommen,

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

Gibt es eine Möglichkeit, die Größe der Videodatei zu reduzieren, während die Videodatei in ein Byte-Array konvertiert wird? bitte antworte :( –

Verwandte Themen