使用android中的parse api无法在解析服务器中上传图像

时间:2016-02-03 07:03:59

标签: android database parse-platform android-imageview

我尝试从图库中选择一个图像并保存在android中的解析云服务器中。 但我无法做到。

我尝试过以下代码:

OnImageView点击事件选择图片:

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

}

解析保存图片的代码:

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

这是我的日志输出:

  

文件======:com.parse.ParseFile@39f19700

2 个答案:

答案 0 :(得分:0)

用此

替换您的代码
ParseFile file = new ParseFile("FoodImage.png", image);

在解析文档中,明确提到您为具有文件扩展名的文件指定名称。这让Parse可以找出文件类型并相应地处理它。 https://parse.com/docs/android/guide#files

答案 1 :(得分:0)

我认为您的图片尺寸问题,可能是您从图库中选择的图片太大而无法保存在parse.com

所以试试这段代码。

这对我有用。

                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....
                      }
                  }