【问题标题】:Saving jpg file with cloud-code Parse-Server使用云代码 Parse-Server 保存 jpg 文件
【发布时间】:2019-02-19 19:35:15
【问题描述】:

我正在尝试在解析服务器上保存带有云代码的 jpg 文件...

在 Android 上我可以用这种方式做到这一点

Bitmap bitmap = ((BitmapDrawable) myImageView.getDrawable()).getBitmap();

ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                    byte [] byteArrayPhotoUpdate = stream.toByteArray();
                    final ParseFile pictureFileParse = new ParseFile( newUserInfo.getObjectId() + ".JPEG",byteArrayPhotoUpdate);

     newUserInfo.put("profile_picture",pictureFileParse);
     newUserInfo.saveInBackground();

但我不知道如何在云代码中执行此操作。我这样调用我的云代码函数

HashMap<String, String> params = new HashMap();

ParseCloud.callFunctionInBackground("myCloudFuncion", params, new FunctionCallback<String>() {
         @Override
          public void done(String aFloat, ParseException e) {

                }
            }); 

但我不知道如何在 hashmap 参数中传递位图。 我已经在互联网上搜索过,但我发现的任何东西都没有帮助,引用有用的东西的链接已经过时了,从旧解析的时代开始......

parse docs 我发现了这个

    var base64 = "V29ya2luZyBhdCBQYXJzZSBpcyBncmVhdCE=";
    var file = new Parse.File("myfile.txt", { base64: base64 });

这让我很困惑,因为我不知道这 2 个“base64”参数是指变量还是 base64 类型

我应该将我的位图转换为 base64 并将其作为参数发送到云代码吗?

如果您已经经历过并且知道如何解决,我会很高兴知道您的解决方案。 谢谢!

【问题讨论】:

    标签: javascript android parse-platform parse-server parse-cloud-code


    【解决方案1】:

    您需要像这样将图像位图转换为 base64:

                Bitmap bitmap = ((BitmapDrawable) img.getDrawable()).getBitmap();
    
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                byte [] byteArrayPhotoUpdate = stream.toByteArray();
                String encodedfile = new String(Base64.encodeBase64(byteArrayPhotoUpdate), "UTF-8");
    

    然后,在参数中发送您的字符串 base64,如下所示:

     HashMap<String, String> params = new HashMap();
     params.put("fileInfo",encodedfile);
     ParseCloud.callFunctionInBackground("saveParseUserInfo", params, new FunctionCallback<String>() {
                        @Override
                        public void done(String aFloat, ParseException e) {
    
                         Log.i("ewaeaweaweaweawe", "done: " + aFloat);
                        }
                    });
    

    现在在您的云代码中,使用它:

    Parse.Cloud.define("saveParseUserInfo", function(request, response) {
                    var userId = request.user.id;
                    var base64 = request.params.fileInfo;
                    var userClass = Parse.Object.extend("User");
                    //create a user object to set ACL
                    var userObject = userClass.createWithoutData(userId);
    
                    //create new ParseObject
                    var userPublicClass = Parse.Object.extend("userPublic");
                    var userPublic = new userPublicClass();
                    var aclAction = new Parse.ACL(userObject);
                    aclAction.setPublicReadAccess(true);
                    userPublic.setACL(aclAction);
                    userPublic.set("name", "name random");
                    userPublic.set("username", "username_random");
                    //Now create a Parse File object
                    var file = new Parse.File("photo.jpeg", { base64: base64 });
                    //set file object in a colum profile_picture
                    userPublic.set("profile_picture",file);
                    //save
                    userPublic.save(null, { useMasterKey: true,  
                    success: function(actionSuccess) {  
    
                        response.success("saved!!");
                    },
                    error: function(action, error) {
                        // Execute any logic that should take place if the save fails.
                        // error is a Parse.Error with an error code and message.
                    response.error(error.message);
                }
                });
    
    
    
    
    
    
                });     
    

    希望对你有帮助。

    【讨论】:

      【解决方案2】:

      如果您不希望使用 Android 需要 API 26 及更高版本的 Base64,则此答案有效。

      我知道 João Armando 已经回答了这个问题,但这是为了像我一样支持 API 26 for Android 之前的版本的其他人的利益。

      附: Base64.encodeBase64(...) 已弃用,现在使用 Base64.getEncoder()...,这需要 API 26。

      解决方案有 3 个关键部分:

      1. 将位图转换为字节数组
      2. 调用云函数时直接将此 byteArray 作为参数发送
      3. 在云代码本身中格式化此 byteArray

      在 Android 中:

      将位图转换为字节[]

      Bitmap bitmap = <Your source>;
      ByteArrayOutputStream stream = new ByteArrayOutputStream();
      bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
      byte[] byteArray = stream.toByteArray();
      

      调用云函数时作为参数发送

      HashMap<String, Object> params = new HashMap<>();
      params.put("imageInByteArray", byteArray);
      
      ParseCloud.callFunctionInBackground("yourCloudFunction", params, new FunctionCallback<Map>() {
        @Override
        public void done(Map object, ParseException e) {
           if(e == null){
             // Success
           } else {
             // Failed
           }
        }
      });
      

      在云端函数/代码中

      取决于您使用的 javascript 版本,代码可能会有所不同。我正在使用后端即服务提供商,它从与承诺相关的代码中得到了改进。无论如何,逻辑应该仍然适用。

      Parse.Cloud.define("reportId", async request => {
        // Retrieve and set values from client app
        const imageInByteArray = request.params.imageInByteArray;
      
        // Format as ParseFile
        var file = new Parse.File("image.png", imageInByteArray);
      
        // Initialize your class, etc.
        ....
      
        // Save your object
        await yourImageObject.save(null, {useMasterKey:true});
      
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-13
        • 1970-01-01
        • 2016-05-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多