【发布时间】:2015-04-22 10:04:54
【问题描述】:
我创建了一个 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 signup 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 的图库中上传选定的图像,因为我的其他参数保存在 parse 中。我也很困惑,因为我从我的第一个活动上传了用户名、密码、纬度、经度和电话。但我在第二个活动中从图库中获取图像。
现在我想知道如何在 parse.com 的同一类中上传所选图像(保存第一个活动参数的位置)。 User 是我在 parse.com 中的类名。
【问题讨论】:
-
您将文件存储在 parse.com 的什么位置?
-
我已将文件存储在 User 类的 profile_pic 列下。
-
在您的代码中,您告诉 parse.com 将 parsefile 对象存储在 profile_pic 中的什么位置?
-
在 else 条件下的第一个活动中,比如这个 user.put("profile_pic", "my profile pic");
-
您的代码正确吗?您在该行中存储字符串,那么如何存储图像?
标签: android parse-platform image-gallery