-1

I try to select an image from gallery and saved in parse cloud server in android. But I am unable to do.

I have tried the following code:

OnImageView Click event choose image:

imageDish.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            try {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(
                        Intent.createChooser(intent, "Select Picture"),
                        SELECT_PICTURE);
            } catch (Exception e) {
                Toast.makeText(getActivity(),
                        "Select Image From Gallery", Toast.LENGTH_LONG)
                        .show();
                // TODO: handle exception
            }

        }
    });

OnActivityResult:

 @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            selectedImageUri = data.getData();

            selectedImagePath = getPath(selectedImageUri);
            System.out.println("Image Path : " + selectedImagePath);
            imageDish.setImageURI(selectedImageUri);
        }
    }
}


@SuppressWarnings("deprecation")
public String getPath(Uri uri) {
    String[] projection = {MediaStore.Images.Media.DATA};
    Cursor cursor = getActivity().managedQuery(uri, projection, null, null, null);
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);

}

Parse Code for save image:

 InputStream imageStream = null;
    try {
                imageStream = getActivity().getContentResolver().openInputStream(
                        selectedImageUri);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

            Bitmap bitmap = BitmapFactory.decodeStream(imageStream);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            // Compress image to lower quality scale 1 - 100
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byte[] image = stream.toByteArray();
            ParseFile file = new ParseFile("FoodImage", image);
            // Upload the image into Parse Cloud
            file.saveInBackground();
            Log.d("File======", "" + file);



      try {
                ParseObject Foodobject = new ParseObject("Food");
                Foodobject.put("FoodName", Name);
                Foodobject.put("ResId", ParseObject.createWithoutData("Restaurant",Res_id);
                Foodobject.put("FoodCategory", ParseObject.createWithoutData("FoodCategory", _CategoryId));
                Foodobject.put("FoodDesc", Des);
                Foodobject.put("Price",priceNumber);
                Foodobject.put("VegOnly", "Y");
                Foodobject.put("IsRecommended", false);
                Foodobject.put("FoodImage", file);
                Foodobject.saveInBackground();
               } catch (Exception ex) {
                Log.e("Error", "" + ex);
            }

Here is my log output:

File======: com.parse.ParseFile@39f19700

6
  • Try this link stackoverflow.com/questions/22186388/… Commented Feb 3, 2016 at 7:06
  • @NikitaSukhadiya yes! i tried...but not working.....:-( Commented Feb 3, 2016 at 7:07
  • So what doesn't work exactly? Commented Feb 3, 2016 at 7:48
  • @Wain When i select image from gallery its not stored in parse.but Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher); when write direct resource image its successfully saved!!! Commented Feb 3, 2016 at 7:57
  • how are you testing that? the save is asynchronous Commented Feb 3, 2016 at 9:35

2 Answers 2

0

Replace your code with this

ParseFile file = new ParseFile("FoodImage.png", image);

In parse docs it is clearly mentioned that you give a name to the file that has a file extension. This lets Parse figure out the file type and handle it accordingly. https://parse.com/docs/android/guide#files

Sign up to request clarification or add additional context in comments.

2 Comments

i tried but not working.. and when i write default image from the drawable its saved....
can you try Bitmap bitmap = BitmapFactory.decodeFile(selectedImagePath); instead of Bitmap bitmap = BitmapFactory.decodeStream(imageStream);
0

i think your issue on image size, it might be your selected image from gallery is too large to save in parse.com

so try this code.

It is working for me .

                ByteArrayOutputStream stream = null;
                Bitmap bitmap = BitmapFactory.decodeFile(picturePath)
                Bitmap newbitmap = Bitmap.createScaledBitmap(bitmap, 200, 200, true);
                stream = new ByteArrayOutputStream();   
                newbitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);

            byte[] image = stream.toByteArray();
            final ParseFile file = new ParseFile("FoodImage.png", image);
            file.saveInBackground(new SaveCallback() {
                @Override
                public void done(ParseException e) {
                    if(e==null){
                     // Your Parse Code....
                      }
                  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.