使用云代码Parse-Server保存jpg文件

问题描述 投票:4回答:1

我正在尝试在解析服务器上保存带有云代码的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
1个回答
2
投票

你需要为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发送到params中,如下所示:

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






            });     

我希望它对你有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.