如何在Parse.com中存储和接收个人资料图片?

时间:2014-07-26 09:22:49

标签: java android parsing parse-platform

我在parse.com上很新。我搜索了几个小时如何为用户创建个人资料图片,但我没有结果。我知道如何将图像上传到Parse.com,但我不知道如何收到它。

这是我上传图片的方式:

  // Save new user data into Parse.com Data Storage
                ParseUser user = new ParseUser();
                user.setUsername(usernametxt);
                user.setPassword(passwordtxt);
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                // Compress image to lower quality scale 1 - 100
                photo.compress(Bitmap.CompressFormat.PNG, 100, stream);
                byte[] image = stream.toByteArray();

                // Create the ParseFile
                ParseFile file = new ParseFile(usernametxt + ".png", image);
                // Upload the image into Parse Cloud
                file.saveInBackground();

                // Create a New Class called "ImageUpload" in Parse
                ParseObject imgupload = new ParseObject("ImageUpload");

                // Create a column named "ImageName" and set the string
                imgupload.put("ImageName", usernametxt);

                // Create a column named "ImageFile" and insert the image
                imgupload.put("ImageFile", file);

                // Create the class and the columns
                imgupload.saveInBackground();

                // Show a simple toast message
                Toast.makeText(RegisterActivity.this, "Image Uploaded",
                        Toast.LENGTH_SHORT).show();

                user.signUpInBackground(new SignUpCallback() {
                   public void done(ParseException e) {
                        if (e == null) {
                            // Show a simple Toast message upon successful registration


                            Intent intent = new Intent(
                                    RegisterActivity.this,
                                    Welcome.class);
                            startActivity(intent);
                            finish();
                        } else {
                            Toast.makeText(getApplicationContext(),
                                   "Sign up Error", Toast.LENGTH_LONG)
                                    .show();
                        }
                    }
               });
           }

        }

这是我收到图像的方式(不工作,导致例外):      ParseQuery query = ParseQuery.getQuery(" ImageUpload");         query.whereEqualTo(" ImageName",currentUser.getUsername());         query.getFirstInBackground(new GetCallback(){

      public void done(ParseObject object, ParseException e) {
        if (object != null) {
            Toast.makeText(Welcome.this, currentUser.getUsername(),
                    Toast.LENGTH_SHORT).show();
            ParseFile file = (ParseFile)object.get("ImageFile");
            file.getDataInBackground(new GetDataCallback() {


            public void done(byte[] data, ParseException e) {
                if (e == null) {

                    bitmap=BitmapFactory.decodeByteArray(data, 0, data.length);
                    profilepic.setImageBitmap(bitmap);
                    //use this bitmap as you want

                } else {
                  // something went wrong
                }
              }
            });

        } else {
            Toast.makeText(getApplicationContext(), "Exception", Toast.LENGTH_SHORT) .show();

        }
      }
    });

如果somone会查看代码,并试图告诉我一些错误,这将对我有所帮助!

抱歉我的英语很差。

1 个答案:

答案 0 :(得分:0)

保存到Parse - 只需调用ParseUser表而不是ParseObject表

Bitmap bitmapImage = ((BitmapDrawable) profilepic.getDrawable()).getBitmap(); // profile pic is the imageview
ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    bitmapImage.compress(Bitmap.CompressFormat.JPEG, 40, stream);

                    byte[] byteArray = stream.toByteArray();

                    ParseFile file = new ParseFile("image.png", byteArray);

                    ParseObject Images = new ParseObject("Images");

                    Images.put("profilepic", file);
                    Images.put("username", Username);

                    Images.saveInBackground(new SaveCallback() {
                        @Override
                        public void done(ParseException e) {
                            if (e == null) {
                                Log.i("AppInfo", "Profile pic success");

                            } else {
                                Log.i("AppInfo", "Profile pic FAIL");
                            }
                        }
                    });

从Parse中检索

ParseQuery<ParseUser> imageQuery = new ParseUser.getQuery();
 imageQuery.whereEqualTo("objectId",ParseUser.getCurrentUser().getObjectId());
    imageQuery.findInBackground(new FindCallback<ParseObject>()
    {
        @Override
        public void done(List<ParseUser> users,ParseException e)
        {
            for(ParseUser user : users)
            {
                ParseFile UserProPicFile = object.getParseFile("ImageColumnName");
                byte[] byteArray = new byte[0];

                byteArray = UserProPicFile.getData();
                Bitmap ProselectBit = BitmapFactory.decodeByteArray(byteArray,0,byteArray.length); //Bitmap with [rofile picture
            }
        }
    });

我希望这会有所帮助