[英]Save an image from the gallery of an Android Phone to Parse - Can't save .jpg photos
After following part of this tutorial and the second answer of this question in SO , I managed to save a photo that I chose from my gallery to my object in Parse . 在遵循了本教程的一部分以及该问题的第二个答案之后 ,我设法将我从画廊中选择的照片保存到Parse中的对象中。
The problem is that the photo that I saved has .PNG extension (it was just a screenshot). 问题是我保存的照片具有.PNG扩展名(这只是屏幕截图)。 When I tried to choose a normal photo from the camera folder, nothing was saved and an exception was occurred.
当我尝试从相机文件夹中选择普通照片时,没有保存任何内容,并且发生了异常。
The extension of ALL the other photos is .jpg NOT .jpeg . 所有其他照片的扩展名是.jpg 不是.jpeg 。
Because of that, i tried to put if
statements, so that I can check the type of the photo. 因此,我尝试放置
if
语句,以便可以检查照片的类型。 The result of the code that is following is that when I choose a .JPG photo, the data type is NULL
. 以下代码的结果是,当我选择.JPG照片时, 数据类型为
NULL
。
But, how can I manage to save the .jpg photos in my Parse Object ? 但是,如何才能将.jpg照片保存在我的Parse Object中?
In my Activity I have 2 buttons. 在我的活动中,我有2个按钮。 when you press the first one (
sign_in
), there is the listener that does correctly all the checks of the other data in my page and then if all data are okay, it calls a function ( postData()
), in which there will be done the saving to parse via objects. 当您按下第一个(
sign_in
)时,有一个侦听器可以正确地检查页面中其他数据,然后如果所有数据都可以,它将调用一个函数( postData()
),其中完成了保存以通过对象进行解析。
The second button is about adding a photo from gallery. 第二个按钮是关于添加图库中的照片。 In my .java activity, I have this exact listener:
在我的.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);
}
});
This is the function that it is being called from the onClick
function of the button: 这是从按钮的
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();
}
}
}
And this is the function that saves the data: 这是保存数据的功能:
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();
}
So .jpeg works and .jpg doesn't? 因此,.jpeg有效,.jpg不有效吗? How about this then (notice you shouldn't compare strings with ==):
然后如何处理(注意,您不应该将字符串与==进行比较):
if (selectedImageType.toUpperCase().equals("JPEG") || selectedImageType.toUpperCase().equals("JPG")){
file = new ParseFile("profile_picture.jpeg", image);
}
Also you can consolidate some earlier code: 您也可以合并一些先前的代码:
if (selectedImageType.toUpperCase().equals("JPEG") || selectedImageType.toUpperCase().equals("JPG")){
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
image = stream.toByteArray();
}
remove your if statements that try to keep track of the "selectedImageType" throughout the process of bitmap creation and image Post to parse.com. 删除试图在位图创建和图像发布过程中始终跟踪“ selectedImageType”的if语句。
Once you have a bitmap, you can simply specify all compression to "Bitmap.CompressFormat.JPEG" and then simply post all jpgs to parse.com. 获得位图后,您可以简单地将所有压缩指定为“ Bitmap.CompressFormat.JPEG”,然后只需将所有jpg发布到parse.com。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.