【问题标题】:Creating base64 encoded JPG string from image from camera for upload从相机的图像创建base64编码的JPG字符串以供上传
【发布时间】:2015-08-18 00:10:10
【问题描述】:

我正在构建一个 Android 应用,它可以拍摄照片,然后将它们上传到 Rails API。

api 需要 base64 编码的原始文件字节,存储为临时文件,以 JPG 格式表示图像。

但是,API 拒绝上传的文件并显示以下错误消息:

<Paperclip::Errors::NotIdentifiedByImageMagickError:

这似乎是由于 Android 应用程序编码失败所致。

我发送的 base64 图像字节如下所示:

仅看一眼就显得无效。

图像是在android中通过使用Camera API和base64编码生成的byteArray的图片来创建的:

String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);

有人知道我在这里做错了什么吗?

【问题讨论】:

  • 你可以试试 NO_WRAP 代替 DEFAULT,Android 的 Base64 实现默认添加了 wrapping。
  • 什么是好奇?
  • 您可以查看文档,NO_WRAP 只是确保没有将行终止符添加到输出值中,这可能是您无法将值解码为有效位图的原因。

标签: android image paperclip bytearray jpeg


【解决方案1】:

点击按钮从相机捕捉图像使用此

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
            ((Activity) context).startActivityForResult(intent, Constants.REQUEST_IMAGE_CAPTURE);

在activity的activityResult上执行以下代码:

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
final ImageView uploadArea = (ImageView) attachmentDialog.findViewById(R.id.uploadArea);
        Bitmap bitmap;
        if (resultCode == RESULT_OK) {
            if (requestCode == 1) {

                File f = new File(Environment.getExternalStorageDirectory().toString());
                for (File temp : f.listFiles()) {
                    if (temp.getName().equals("temp.jpg")) {
                        f = temp;
                        break;
                    }
                }
                try {
                    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();

                    bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),
                            bitmapOptions);

                    Matrix matrix = new Matrix();

                    matrix.postRotate(-90);

                    Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
                    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                    rotatedBitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
                    byte[] attachmentBytes = byteArrayOutputStream.toByteArray();

                    String attachmentData = Base64.encodeToString(attachmentBytes, Base64.DEFAULT);


                    uploadArea.setImageBitmap(rotatedBitmap);

                    String path = android.os.Environment
                            .getExternalStorageDirectory()
                            + File.separator
                            + "CTSTemp" + File.separator + "default";
                    f.delete();
                    OutputStream outFile = null;
                    File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");
                    try {
                        outFile = new FileOutputStream(file);
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile);
                        outFile.flush();
                        outFile.close();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
}

希望对您有所帮助,如需更多信息,请询问

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-06
    • 2015-04-30
    相关资源
    最近更新 更多