从相机的图像创建base64编码的JPG字符串以进行上传

时间:2015-06-03 16:38:47

标签: android image paperclip bytearray jpeg

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

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

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

<Paperclip::Errors::NotIdentifiedByImageMagickError:

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

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

仅通过查看它就显得无效。

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

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

任何人都知道我在这里做错了吗?

1 个答案:

答案 0 :(得分:2)

按钮单击以从相机捕获图像使用此

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();
                }
            }
}

我希望这对您有所帮助,如有任何进一步的信息,请询问