Marialena
Marialena

Reputation: 817

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.

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

Upvotes: 0

Views: 1297

Answers (2)

Robert Rowntree
Robert Rowntree

Reputation: 6289

remove your if statements that try to keep track of the "selectedImageType" throughout the process of bitmap creation and image Post to parse.com.

Once you have a bitmap, you can simply specify all compression to "Bitmap.CompressFormat.JPEG" and then simply post all jpgs to parse.com.

Upvotes: 1

nasch
nasch

Reputation: 5498

So .jpeg works and .jpg doesn't? 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();
                }

Upvotes: 1

Related Questions