在完成this tutorial和second answer of this question in SO的部分后,我设法将我从图库中选择的照片保存到Parse中的对象中。
问题是我保存的照片有 .PNG 扩展名(它只是一个屏幕截图)。 当我尝试从相机文件夹中选择普通照片时,没有保存任何内容并发生异常。
所有其他照片的扩展名为 .jpg NOT .jpeg 。
因此,我试图放置if
语句,以便我可以检查照片的类型。
以下代码的结果是,当我选择 .JPG 照片时,数据类型为NULL
。
但是,我如何设法将.jpg照片保存在我的Parse对象中?
在我的活动中,我有2个按钮。当你按下第一个(sign_in
)时,有一个监听器正确地检查了我页面中其他数据的所有检查,然后如果所有数据都正常,它会调用一个函数(postData()
) ,其中将完成保存以通过对象进行解析。
第二个按钮是关于从图库添加照片。在我的.java活动中,我有这个确切的监听器:
picture.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startActivityForResult(new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI), GET_FROM_GALLERY);
}
});
这是从按钮的onClick
功能调用它的功能:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//Detects request codes
if(requestCode==GET_FROM_GALLERY && resultCode == Activity.RESULT_OK) {
Uri selectedImage = data.getData();
selectedImageType =data.getType();
Toast.makeText(SignUpActivity.this, "Type: "+selectedImageType,
Toast.LENGTH_SHORT).show();
try {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);
// Convert it to byte
ByteArrayOutputStream stream = new ByteArrayOutputStream();
// Compress image to lower quality scale 1 - 100
if (selectedImageType == "JPEG"){
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
// bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
image = stream.toByteArray();
}
else if (selectedImageType == "JPG" || selectedImageType == "jpg"){
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
// bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
image = stream.toByteArray();
}
else if (selectedImageType == "PNG") {
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
// bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
image = stream.toByteArray();
}
else{
Toast.makeText(SignUpActivity.this, "Please pick a JPEG or PNG photo!",
Toast.LENGTH_SHORT).show();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这是保存数据的功能:
public void postData(final String username,final String password,final String email,final String gender,final String age) {
ParseObject user = new ParseObject("users");
user.put("username", username);
user.put("password", password);
user.put("email", email);
user.put("gender", gender);
user.put("age_category", age);
user.put("admin", false);
ParseFile file = null;
if (selectedImageType == "JPEG"){
file = new ParseFile("profile_picture.jpeg", image);
}
else if (selectedImageType == "JPG" || selectedImageType == "jpg"){
file = new ParseFile("profile_picture.jpg", image);
}
else if (selectedImageType == "PNG"){
file = new ParseFile("profile_picture.png", image);
}
else{
// Show a simple toast message
Toast.makeText(SignUpActivity.this, "Please pick a JPEG or PNG photo!",
Toast.LENGTH_SHORT).show();
}
// Upload the image into Parse Cloud
file.saveInBackground();
user.put("photo", file);
// Create the class and the columns
user.saveInBackground();
// Show a simple toast message
Toast.makeText(SignUpActivity.this, "Image Uploaded",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(SignUpActivity.this, LoginActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.push_down_in, R.anim.push_down_out);
//finish();
}
答案 0 :(得分:1)
所以.jpeg有效,.jpg没有?那么怎么样(注意你不应该将字符串与==进行比较):
if (selectedImageType.toUpperCase().equals("JPEG") || selectedImageType.toUpperCase().equals("JPG")){
file = new ParseFile("profile_picture.jpeg", image);
}
此外,您可以合并一些早期代码:
if (selectedImageType.toUpperCase().equals("JPEG") || selectedImageType.toUpperCase().equals("JPG")){
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
image = stream.toByteArray();
}
答案 1 :(得分:1)
删除在整个位图创建过程中尝试跟踪“selectedImageType”的if语句,并将图像发布到parse.com。
一旦有了位图,就可以简单地将所有压缩指定为“Bitmap.CompressFormat.JPEG”,然后将所有jpgs发布到parse.com。