使用Parse Code将图像/文件保存到Parse Cloud中

时间:2015-11-12 08:08:27

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

我有这个代码(Java / Android),它获取位图,将其转换为字节数组,然后将其放入一个称为“图像”的地图中。并最终通过调用" createcard"功能:

    public static void createCard(final String nametext, final String initialbalancetext, String databaseclass, String cardnotes, String cardtype, Bitmap picture) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    picture.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] byteArray = stream.toByteArray();
    Map<String, Object> map = new HashMap<>();
    map.put("cardname", nametext);
    map.put("balance", Double.valueOf(initialbalancetext));
    map.put("database", databaseclass);
    map.put("cardnotes", cardnotes);
    map.put("picture", byteArray);
    // Check if network is connected before creating the card
    if (CardView.isNetworkConnected) {
        ParseCloud.callFunctionInBackground("createcard", map, new FunctionCallback<String>() {
            @Override
            public void done(String s, ParseException e) {
                // COMPLETE
                CardListCreator.clearadapter();
                CardListAdapter.queryList();
            }
        });
    }

这是我的#34;创建卡&#34;功能(Javascript / Parse Cloudcode)。据说这需要拍摄照片&#39;键和抓取字节数组,并尝试保存它:

Parse.Cloud.define("createcard", function(request, response){
var cardname = request.params.cardname;
var balance = request.params.balance;
var database = request.params.database;
var cardnotes = request.params.cardnotes;
var picture = request.params.picture;

var picturename = "photo.jpg";
var parseFile = new Parse.File(picturename, picture);
parseFile.save().then(function(parseFile){
    var currentuser = Parse.User.current();
    var CurrentDatabase = Parse.Object.extend(database);

    var newObject = new CurrentDatabase;
    newObject.set("cardname", cardname);
    newObject.set("balance", balance);
    newObject.set("user", currentuser);
    newObject.set("cardnotes", cardnotes);
    newObject.set("cardpicture", parseFile);
    newObject.save(null, {
        success: function(newObject){
            console.log(newObject);
            response.success(newObject + " successfully created");
        }, error: function(newObject, error){
            console.log(error);
            response.error(error+" error");
        }
    })
});
});

现在我的问题是该功能根本不起作用。我不知道为什么我的console.log实际上没有记录任何东西。有没有人有任何想法?

2 个答案:

答案 0 :(得分:3)

我希望这段代码能够提出你的问题。

   private void Update(){

    ParseQuery<ParseObject> query = ParseQuery.getQuery("Table Name");
    query.getInBackground(parsekey, new GetCallback<ParseObject>() {
        public void done(ParseObject gameScore, ParseException e) {


            if (e == null) {

                if(camera!=null) {
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    camera.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                    byte imageInByte[] = stream.toByteArray();
                    encodedImage = Base64.encodeToString(imageInByte, Base64.DEFAULT);
                    image = new ParseFile("image.txt", imageInByte);                  

                }else{
                    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.user_icon);
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                    byte imageInByte[] = stream.toByteArray();
                    encodedImage = Base64.encodeToString(imageInByte, Base64.DEFAULT);
                    image = new ParseFile("image.txt", imageInByte);
                }




              findViewById(R.id.editName)).getText().toString(),Profile.this);
            gameScore.put("name", ((EditText) findViewById(R.id.editName)).getText().toString().trim());
            gameScore.put("gender",gender_val);
            gameScore.put("country",country_val);
            gameScore.put("dateofbirth",((TextView) findViewById(R.id.editdate)).getText().toString().trim());
            gameScore.put("profession",profession);
            gameScore.put("image",image);

            gameScore.saveInBackground();


                Toast.makeText(Profile.this,"SuccessFull Updated",Toast.LENGTH_LONG).show();

            }else{
                Log.e("erroreeeee", DataBase.getUserObjectId(Profile.this)+"  "+e.getMessage());
            }

        }

    });
}

答案 1 :(得分:0)

对于未来的所有人:您希望将图像放入byte []并将其放入地图中。您不需要将其编码为字符串,除非您想在云代码中对其进行解码。在cloudcode上,请求你上传的byte []的参数,然后将其放入ParseFile,然后最后调用save。