所有设备的图像质量都不好

时间:2018-02-12 12:07:09

标签: android bitmap server base64 android-volley

我试图通过将图像转换为base64字符串,将图像上传到Android应用程序中的服务器。在这种情况下,当我尝试上传图像时,我的图像质量大大降低并且非常模糊。你能帮我解决这个问题。

我改变o2.inSampleSize = 10它在摩托罗拉,三星,联想中运作良好但不适用于所有设备。请建议我为什么这么开心..

public Bitmap decodeFile(File f, int sampling) {
    try {
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(
                new FileInputStream(f.getAbsolutePath()), null, o2);

        o2.inSampleSize = sampling;      //sampling=10
        o2.inTempStorage = new byte[48 * 1024];

        o2.inJustDecodeBounds = false;
        Bitmap bitmap = BitmapFactory.decodeStream(
                new FileInputStream(f.getAbsolutePath()), null, o2);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        bitmap = rotateImage(bitmap, f.getAbsolutePath());
        //img = ConvertBitmapToString(bitmap);

        return bitmap;

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
    }
    return null;
}

将位图转换为base64

  ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

  image.compress(Bitmap.CompressFormat.WEBP,100,byteArrayOutputStream);

  String encodeImage = Base64.encodeToString(byteArrayOutputStream.toByteArray(),Base64.DEFAULT);

任何帮助都将受到高度赞赏

2 个答案:

答案 0 :(得分:2)

使用Glide准备图像:.asBitmap().toBytes().into()

然后上传它。 有关详细信息,请访问此链接:https://github.com/bumptech/glide#glide

或此链接:https://github.com/bumptech/glide/issues/1192

答案 1 :(得分:1)

不要转换它,上传文件改造,okhttp。 为获取POST请求的url创建一个接口

 RequestBody requestFile =
        RequestBody.create(
                     MediaType.parse(getContentResolver().getType(fileUri)),
                     file
         );MultipartBody.Part body =
        MultipartBody.Part.createFormData("picture", file.getName(), requestFile);

然后创建一个包含文件的RequesBody var和MultiPartBody var来保存文件名

RequestBody description =
        RequestBody.create(
                okhttp3.MultipartBody.FORM, "someFile");

和文件说明:

Call<ResponseBody> call = service.upload(description, body);
call.enqueue(new Callback<ResponseBody>() {
    @Override
    public void onResponse(Call<ResponseBody> call,
                           Response<ResponseBody> response) {
        Log.v("Upload", "success");
    }

    @Override
    public void onFailure(Call<ResponseBody> call, Throwable t) {
        Log.e("Upload error:", t.getMessage());
    }
});

然后通过改装上传

react-native 0.47

了解更多信息,请查看this

相关问题