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.
Gibt es einen Grund, warum Sie Bild entschlüsseln und dann wieder komprimiert werden? – harism
@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
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