【发布时间】:2019-08-05 14:30:45
【问题描述】:
我正在开发一个具有Parse Server Android SDK 的应用程序,并且我知道如何将视频文件转换为bytes[] 数组,这是我使用的代码:
private byte[] convertVideoToBytes(Uri uri){
byte[] videoBytes = null;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileInputStream fis = new FileInputStream(new File(getRealPathFromURI(uri)));
byte[] buf = new byte[1024];
int n;
while (-1 != (n = fis.read(buf)))
baos.write(buf, 0, n);
videoBytes = baos.toByteArray();
} catch (IOException e) { e.printStackTrace(); }
return videoBytes;
}
// GET VIDEO PATH AS A STRING -------------------------------------
public String getRealPathFromURI(Uri contentUri) {
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = ctx.getContentResolver().query(contentUri, filePathColumn, null, null, null);
assert cursor != null;
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
Log.i(TAG, "VIDEO PATH: " + filePath);
return filePath;
}
但我无法上传 GIF 文件,我试过这段代码,但它没有做任何事情,我的 .gif 文件在我的数据库中的单元格在保存后是空的在后台使用saveInBackground():
Uri path = Uri.parse("android.resource://" + BuildConfig.APPLICATION_ID + "/" + R.drawable.my_animated_gif_from_drawable);
String gifPath = path.toString();
File file = new File(gifPath);
Log.i(TAG, "GIF PATH: " + file.getAbsolutePath());
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
int read;
byte[] buff = new byte[1024];
while ((read = in.read(buff)) > 0) { out.write(buff, 0, read); }
out.flush();
byte[] bytes = out.toByteArray();
ParseFile gifFile = new ParseFile(gifName + ".gif", bytes);
bObj.put(BUZZ_GIF, gifFile);
} catch (FileNotFoundException ignored) {
} catch (IOException ignored) { }
有谁知道如何将 .gif 文件上传为 ParseFile,或者只是将其转换为 bytes[] 以便我可以使用 new ParseFile 功能?
【问题讨论】:
标签: java android gif parse-server parse-android-sdk