在Android中的ParseFile中将图库中的图像放入

时间:2014-11-26 10:25:51

标签: android image bitmap parse-platform

我正在开发一个parse.com Android应用程序,我将预定义/默认图像放在ParseFile中并将其发送到服务器。现在我想在图库中为用户选择添加图像。

我的代码如下:

//It is my default image which i am getting from drawable.
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.usman);

ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] image = stream.toByteArray();

ParseFile file = new ParseFile("profileImage.png", image);
// Upload the image into Parse Cloud
file.saveInBackground();
// Create a column named "profileImage" and insert the image
Constants.user.put("profileImage", file);
Constants.user.saveInBackground(new SaveCallback() {

    @Override
    public void done(ParseException e) {

        // dismissLoadingDialog();
        if (e == null) {
            Toast.makeText(getApplicationContext(),"User Profile Has Been Updated!",Toast.LENGTH_SHORT).show();
        } else {

            Logs.e(getClass().getName(), e.toString());
            Toast.makeText(getApplicationContext(), "Exception/n" + e, Toast.LENGTH_SHORT).show();
            // showMessage(e.getMessage());
        }
    }
});

1 个答案:

答案 0 :(得分:2)

试试这个......

 public void setImage( Bitmap map){


    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    // Compress image to lower quality scale 1 - 100
    map.compress(Bitmap.CompressFormat.JPEG, 100, stream);
    byte[] image = stream.toByteArray();

    // Create the ParseFile
    ParseFile file  = new ParseFile("picture_1.jpeg", image);
     // Upload the image into Parse Cloud
    ParseUser user = ParseUser.getCurrentUser();
    user.put("profilePic",file);

user.saveInBackground(new SaveCallback() {
    @Override
    public void done(ParseException e) {

    }
});

}