vendredi 9 janvier 2015

Save an image from the gallery of an Android Phone to Parse - Can't save .jpg photos


Vote count:

0




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.


enter image description here


The problem is that the photo that I saved has .PNG extension (it was just a screenshot). 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.


Because of that, i tried to put if statements, so that I can check the type of the photo. The result of the code that is following is that when I choose a .JPG photo, the data type is NULL.


But, how can I manage to save the .jpg photos in my Parse Object ?




In my Activity I have 2 buttons. 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.


The second button is about adding a photo from gallery. In my .java activity, I have this exact listener:



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:



@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();
}


asked 26 secs ago







Save an image from the gallery of an Android Phone to Parse - Can't save .jpg photos

Aucun commentaire:

Enregistrer un commentaire