android - 通过 multipart 将 byte[] 图像上传到服务器
问题描述
我正在开发 Android 上的图像上传功能,我正在使用这两个库: https ://github.com/natario1/CameraView
https://github.com/gotev/android-upload-service
所以,根据 CameraView 库,我可以得到这样的照片:
mCameraView.addCameraListener(new CameraListener() {
@Override
public void onPictureTaken(byte[] jpeg) {
super.onPictureTaken(jpeg);
}
});
所以我有我的图片作为字节数组。这里的问题是如何通过多部分将其上传到我的服务器?我的后端已准备好接受文件。所以我相信我必须将我的 byte[] 转换为文件?
编辑 1:抱歉,这个问题很不清楚,问题应该缩小到“如何将字节 [] 写入文件。
解决方案
首先,您必须将字节存储到文件中。存储图像后转换为 Multipart
File file = new File(fileUri);
RequestBody reqFile = RequestBody.create(MediaType.parse("image*//*"), file);
MultipartBody.Part body = MultipartBody.Part.createFormData(AppConstants.IMAGE, file.getName(), reqFile);
private File saveImage(byte[] bytes, int rotate) {
try {
Bitmap bitmap = decodeSampledBitmapFromResource(bytes, bytes.length, 800, 600, rotate);
return createFile(bitmap);
} catch (Exception e) {
Log.e("Picture", "Exception in photoCallback", e);
}
return null;
}
public Bitmap decodeSampledBitmapFromResource(byte[] bytes, int length, int reqWidth,
int reqHeight, int rotate) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(bytes, 0, length, options);
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
options.inJustDecodeBounds = false;
Bitmap bm = BitmapFactory.decodeByteArray(bytes, 0, length, options);
Bitmap rotatedBitmap = null;
if (isFrontfaceing) {
if (rotate == 90 || rotate == 270) {
rotatedBitmap = rotateImage(bm, -rotate);
} else {
rotatedBitmap = rotateImage(bm, rotate);
}
} else {
rotatedBitmap = rotateImage(bm, rotate);
}
rotatedBitmap = Bitmap.createScaledBitmap(rotatedBitmap, reqWidth, reqHeight, true);
ByteArrayOutputStream out = new ByteArrayOutputStream();
rotatedBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
return rotatedBitmap;
}
public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth,
int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
|| (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
public File createFile(Bitmap bitmap) {
File photo =
new File(getWorkingDirectory().getAbsolutePath()+"yourFileName" + ".jpg");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(photo.getPath());
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
return null;
}
return photo;
}
推荐阅读
- html - Websocket 打开 example.com/? 什么时候应该保留在 example.com 上(使用 socket.io)
- discord.js - Discord.js如何通过id检查id_blacklist数组中的服务器上是否有用户?
- java - 我正在转换日期,但它会在月份中多加一个点如何处理
- algorithm - 增强二叉搜索树以在 O(1) 时间内实现最小和最大操作
- ruby-on-rails - 通过重命名的关联使用 Rails 的 build() 方法
- c++ - C ++中线程中的意外输出
- shell - 如何在 linux awk 中连接变量
- powershell - 将 Log Analytics REST API 与 MSAL 而非 ADAL 结合使用
- python-3.x - python3多处理map_async没有运行所有任务
- sql-server - SSMS 导入数据(使用 Excel)错误:SSMS 读取我的字段有 255 个字符,但我有更少