【问题标题】:How to store and receive a profile picture in Parse.com?如何在 Parse.com 中存储和接收个人资料图片?
【发布时间】:2014-09-18 02:55:45
【问题描述】:

我是 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 查询 = 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();

        }
      }
    });

如果有人会查看代码并尝试给我一个线索我做错了什么,那将对我很有帮助!

对不起,我的英语不好。

【问题讨论】:

    标签: java android parsing parse-platform


    【解决方案1】:

    保存到 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");
                                }
                            }
                        });
    

    从解析中检索

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

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-20
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      相关资源
      最近更新 更多