Android 通过多部分将字节[]图像上载到服务器

Android 通过多部分将字节[]图像上载到服务器,android,camera-view,Android,Camera View,我正在Android上开发图像上传功能,我正在使用以下两个库: 所以,根据CameraView图书馆,我可以得到这样的照片: mCameraView.addCameraListenernew CameraListener{ @凌驾 公共无效onPictureTakenbyte[]jpeg{ super.onPictureTakenjpeg; } }; 首先,您必须将字节存储到文件中。存储图像后,将其转换为多部分 File file = new File(fileUri);

我正在Android上开发图像上传功能,我正在使用以下两个库:

所以,根据CameraView图书馆,我可以得到这样的照片:

mCameraView.addCameraListenernew CameraListener{ @凌驾 公共无效onPictureTakenbyte[]jpeg{ super.onPictureTakenjpeg; }
}; 首先,您必须将字节存储到文件中。存储图像后,将其转换为多部分

File file = new File(fileUri);
            RequestBody reqFile = RequestBody.create(MediaType.parse("image*//*"), file);
            MultipartBody.Part body =  MultipartBody.Part.createFormData(AppConstants.IMAGE, file.getName(), reqFile);

基本上,我们只需要将字节[]写入文件。首先,我为它创建占位符文件。这是从谷歌官方文档中截取的代码

私有文件createImageFile引发IOException{ //创建图像文件名 字符串时间戳=新SimpleDataFormatyyyymmdd\u HHmmss.formatnew Date; 字符串imageFileName=JPEG+时间戳+; 文件storageDir=getexternalfilesdireEnvironment.DIRECTORY\u图片; File image=File.createTempFile imageFileName, jpg先生, storageDir ; 返回图像;
}只需使用HttpUrlConnection将字节发送到服务器。您不需要先创建文件。为什么要先将jpg转换为位图?调整大小了吗?然后轮换?太多不需要的代码了。您应该直接将这些字节写入文件,而不使用中间位图。是的……这就是我用来调整图像大小的方法。您可以直接将字节保存到file@himangi,谢谢你的回答,这对我解决问题帮助很大。基本上,我认为我的问题的范围是错误的,我需要的只是学习如何将byte[]写入文件,这相当简单。首先,我创建了占位符文件。。无益。不要使用File.createTempFile。该文件将由新的FileOutputStream创建。您只需要一个文件名/路径。@greenapps实际上不需要写入任何临时文件-您只需要传递一个接受字节数组的自定义RequestBody类,并覆盖contentType和writeTomethods@pskink请告诉OP,他可以用。您也可以将其作为答案发布。@greenapps OP已经说过:谢谢您的回答,它帮助我解决了很多问题。基本上我认为我的问题的范围是非常错误的,我需要的只是学习如何将byte[]写入文件,这相当简单,所以我不认为我可以改变他的想法,但我认为如果你需要在将来做这样的事情,我可以改变你的想法future@pskink非常有趣。您可能没有看到我的第一条评论:只需使用HttpUrlConnection将字节发布到服务器。您不需要先创建文件。的确我也可以把它作为一个答案;-。
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;
}