如何将图片从图库上传到parse.com?

时间:2016-01-09 15:18:55

标签: android parse-platform

我正在尝试将图片中的照片上传到我的解析云中,但我无法弄清楚这是我的代码以及到目前为止我所做的事情。   我到处看都还找不到解决办法,无法上传照片: 请帮帮我。

 public void loadImagefromGallery(View view) {
    // Create intent to Open Image applications like Gallery, Google Photos
    Intent galleryIntent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    // Start the Intent
    startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    try {
        // When an Image is picked
        if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
                && null != data) {
            // Get the Image from data

            Uri selectedImage = data.getData();
           myBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);

            byte[] scaledData = bos.toByteArray();
            photoFile = new ParseFile("my_photo.jpg", scaledData);
            photoFile.saveInBackground(new SaveCallback() {

                public void done(ParseException e) {
                    if (e != null) {
                        Toast.makeText(getApplicationContext(),
                                "Error saving: " + e.getMessage(),
                                Toast.LENGTH_LONG).show();
                    } else {
          // do something
                    }
                }


            });

1 个答案:

答案 0 :(得分:1)

将ParseObject保存在后台

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

// ParseObject
  ParseObject pObject = new ParseObject("ExampleObject");
// Create a column named "ImageName" and set the string
  pObject.put("ImageName", "image name here");
// Create a column named "ImageFile" and insert the image
  pObject.put("ImageFile", file);
  pObject.saveInBackground(); // asynchronous, no callback

使用回调保存在后台

pObject.saveInBackground(new SaveCallback () {
@Override
   public void done(ParseException ex) {
    if (ex == null) {
        isSaved = true;
    } else {
        // Failed
        isSaved = false;
    }
  }
});