-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

Viveka Patel
  • 666
  • 7
  • 17

2 Answers2

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

Ajinkya
  • 1,057
  • 7
  • 18
  • i tried but not working.. and when i write default image from the drawable its saved.... – Viveka Patel Feb 03 '16 at 07:37
  • can you try Bitmap bitmap = BitmapFactory.decodeFile(selectedImagePath); instead of Bitmap bitmap = BitmapFactory.decodeStream(imageStream); – Ajinkya Feb 03 '16 at 08:31
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....
                      }
                  }
Nikita Sukhadiya
  • 367
  • 2
  • 4
  • 10