如何在android中的parse.com上上传图库图片?

时间:2015-04-22 10:04:55

标签: android parse-platform image-gallery

我创建了一个Android应用程序,其中存储了parse.com上的用户名,电话,密码,经度和经度。当我点击提交按钮时,所有这些值都成功存储在parse.com上。这是代码

LoginSigupActivity.java

public void onClick(View arg0) {
				usernametxt = username.getText().toString();
				passcodetxt = passcode.getText().toString();
				phonetxt = phone.getText().toString();

				if (usernametxt.equals("") && passcodetxt.equals("")) {
					Toast.makeText(getApplicationContext(),
							"Please complete the sign up form",
							Toast.LENGTH_LONG).show();

				} else {
					// Save new user data into Parse.com Data Storage
					ParseUser user = new ParseUser();
					user.setUsername(usernametxt);
					user.setPassword(passcodetxt);
					user.put("phone", phone.getText().toString());
					user.put("passcode", passcode.getText().toString());
					//user.put("profile_pic", "my profile pic");

					// code to send the current latlong using parse.com
					user.put("latitude", gpsTracker.getLatitude() + "");
					user.put("longitude", gpsTracker.getLongitude() + "");
					
					
					user.signUpInBackground(new SignUpCallback() {
						public void done(ParseException e) {
							if (e == null) {

								Intent intent = new Intent(
										LoginSignupActivity.this, Welcome.class);
								startActivity(intent);

								Toast.makeText(getApplicationContext(),
										"Successfully Signed up !!!",
										Toast.LENGTH_LONG).show();
							} else {
								Toast.makeText(getApplicationContext(),
										"Sign up Error", Toast.LENGTH_LONG)
										.show();
							}
						}
					});
				}

			}

		});

	}
}

现在当第二个活动在这里打开时,我使用了图像视图,获取图像按钮和上传图像按钮。点击获取图像按钮图像在图像视图中正确获取但是当我尝试上传时它不上传。

以下是代码:

Welcome.java

selectImageButton = (Button) findViewById(R.id.selectImageButton);
		// initlize gps tracker
		gpsTracker = new GPSTracker(this);

		String picturePath = PreferenceManager
				.getDefaultSharedPreferences(this).getString("picturePath", "");
		if (!picturePath.equals("")) {
			ImageView imageView = (ImageView) findViewById(R.id.uploadImage);
			imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
		}

		selectImageButton = (Button) findViewById(R.id.selectImageButton);
		selectImageButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent i = new Intent(
						Intent.ACTION_PICK,
						android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
				startActivityForResult(i, RESULT_LOAD_IMAGE);
			}
		});
		
	

	   uploadButton = (Button) findViewById(R.id.uploadButton);

		uploadButton.setOnClickListener(new View.OnClickListener() {
			@SuppressWarnings("null")
			@Override
			public void onClick(View arg0) {  
				
				ParseUser user = new ParseUser();
				Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
				// Convert it to byte
				ByteArrayOutputStream stream = new ByteArrayOutputStream();
				// Compress image to lower quality scale 1 - 100
				bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
				byte[] image = stream.toByteArray();

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

		
				});*/

				user.signUpInBackground(new SignUpCallback() {
					public void done(ParseException e) {
						if (e == null) {
							Toast.makeText(getApplicationContext(),
									"Successfully Signed up !!!",
									Toast.LENGTH_LONG).show();
						} else {
							Toast.makeText(getApplicationContext(),
									"Sign up Error", Toast.LENGTH_LONG).show();
						}
					}
				});

			}
		});

		// Retrieve current user from Parse.com
		ParseUser currentUser = ParseUser.getCurrentUser();
       // Convert currentUser into String
		String struser = currentUser.getUsername().toString();
       // Locate Button in welcome.xml
		logout = (Button) findViewById(R.id.logout);
        // Logout Button Click Listener
		logout.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				Intent intent = new Intent(Welcome.this,
						LoginSignupActivity.class);
				startActivity(intent);
			}
		});
	}

   
  
   

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	super.onActivityResult(requestCode, resultCode, data);

		if (resultCode == RESULT_OK) {

			Uri selectedImageUri = data.getData();

			// GET IMAGE PATH
			imagePath = getPath(selectedImageUri);

			// IMAGE NAME
			imageName = imagePath.substring(imagePath.lastIndexOf("/"));

			// Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
			//
			// // DISPLAY IMAGE
			// imageView.setImageBitmap(bitmap);
			// imageLocationTextView.setText("File path :" + imagePath);

		}

	}

	private String getPath(Uri uri) {
		String[] projection = { MediaStore.Images.Media.DATA };
		Cursor cursor = managedQuery(uri, projection, null, null, null);
		int column_index = cursor
				.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
		cursor.moveToFirst();
		return cursor.getString(column_index);
	}
}

现在我想知道如何从parse.com上的库中上传所选图像。我的其他参数保存在解析上。我也很困惑,因为我从我的First活动上传了用户名,密码,lat,long和phone。但我在第二次活动中从画廊获得图像。

现在我想知道如何在parse.com(保存第一个活动参数)的同一个类中上传所选图像。用户是parse.com中的我的班级名称。

请帮助我。非常感谢。

2 个答案:

答案 0 :(得分:0)

检查以下代码,您会明白

ParseObject  ParseObj = new ParseObject ("YOUR_TABLE_NAME");
ParseFile file = new ParseFile("androidbegin.png", image);
ParseObj.put("profile_pic", file);
ParseObj.saveInBackground(new SaveCallback() {
    @Override
    public void done(ParseException arg0) {
        if (arg0 == null) {
            Log.i("LOG_OUTPUT", "Data saved in sever");
        } else {
            Log.e("LOG_OUTPUT", "Data not saved in server");

        }
    }
});
file.saveInBackground(new SaveCallback() {
    @Override
    public void done(ParseException arg0) {
        try {
            if (arg0 == null) {
                Log.e("LOG_OUTPUT", "imageFile is saved");
            } else {
                Log.e("LOG_OUTPUT", "imageFile is not saved");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

});

答案 1 :(得分:0)

这对我有用

                       imageView1.buildDrawingCache();
                        Bitmap bitmap =imageView1.getDrawingCache();
                            ByteArrayOutputStream stream = new ByteArrayOutputStream();
                            // Compress image to lower quality scale 1 -
                            // 100
                            bitmap.compress(Bitmap.CompressFormat.PNG,
                                    100, stream);
                            byte[] image = stream.toByteArray();

                            // Create the ParseFile
                            ParseFile file = new ParseFile(
                                    "Profile.png", image);

                            // file.getUrl();
                            // Upload the image into Parse Cloud
                            file.saveInBackground();

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

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

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

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

                            imgupload
                                    .saveInBackground(new SaveCallback() {
                                        @Override
                                        public void done(
                                                ParseException e) {
                                            if (e == null) {
                                                // Success!

                                                String objectId = imgupload
                                                        .getObjectId();
                                                Editor editor = sharedpref_objid
                                                        .edit();
                                                editor.putString(
                                                        "objid",
                                                        objectId);

                                                Toast.makeText(
                                                        getApplicationContext(),
                                                        objectId,
                                                        Toast.LENGTH_SHORT)
                                                        .show();

                                                editor.commit();

                                            } else {
                                                // Failure!
                                            }
                                        }
                                    });