使用Parse作为后端上传/保存所选图像

时间:2014-04-02 10:22:49

标签: java android eclipse parsing

好的家伙,所以在花了一天试图弄清楚如何上传图片到解析服务器后,我终于决定寻求你的帮助。我没有找到关于如何做到这一点的完整示例。

我希望能做的是:

  1. 从图库中选择图片(已经这样做了)
  2. 加载到inageView(已经这样做)
  3. 在onClick事件中将所选图片上传到Parse服务器(我的问题)
  4. 到目前为止,您有我的代码段,但它无效。

    private static int RESULT_LOAD_IMAGE = 1;
    
    
    mSubmitJobBTN.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
    
                createJob(); //this method will send data to Parse
    
            }
        });
    
    
    private void addJob(final String mUsernameText, String mJobNameText,
            String mJobDescriptionText, String mJobPriceText) {
    
        /*Bitmap bitmap = BitmapFactory.decodeFile("picturePath");
        // Convert it to byte
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        // Compress image to lower quality scale 1 - 100
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] image = stream.toByteArray();
    
        try {
            image = readInFile(path);
        } catch (Exception e) {
            e.printStackTrace();
        }*/
    
        ParseUser user = ParseUser.getCurrentUser();
    
        anunt.put("username", "andrei");
        anunt.put("jobName", mJobNameText);
        anunt.put("jobDescription", mJobDescriptionText);
        anunt.put("jobPrice", mJobPriceText);
    
    
    
        /*// Create a column named "jobPicture" and set the string
        anunt.put("jobPicture", "picturePath");
    
        // Create the ParseFile
        ParseFile file = new ParseFile("picturePath", image);
        // Upload the image into Parse Cloud
        file.saveInBackground();
    
        // Create a column named "ImageFile" and insert the image
        anunt.put("ImageFile", file);*/
    
    
        anunt.saveInBackground(new SaveCallback() {
            public void done(ParseException e) {
                if (e == null) {
                    Toast.makeText(getApplicationContext(),
                            "Job succesfully posted!", Toast.LENGTH_LONG)
                            .show();
                    Intent in = new Intent(getApplicationContext(),
                            JobsListActivity.class);
                    in.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(in);
                    finish();
    
                } else {
    
                    Toast.makeText(getApplicationContext(),
                            "Sign up error, please check all the fields",
                            Toast.LENGTH_LONG).show();
                }
            }
    
        });
    }
    
    
        public byte[] readInFile(String path) throws IOException {
        byte[] data = null;
        File file = new File(path);
        InputStream input_stream = new BufferedInputStream(new FileInputStream(
                file));
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        data = new byte[16384]; // 16K
        int bytes_read;
        while ((bytes_read = input_stream.read(data, 0, data.length)) != -1) {
            buffer.write(data, 0, bytes_read);
        }
        input_stream.close();
        return buffer.toByteArray();
    
    }***strong text***
    
        @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    
        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
                && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
    
            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();
    
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            PreferenceManager.getDefaultSharedPreferences(this).edit()
                    .putString("picturePath", picturePath).commit();
            cursor.close();
    
            ImageView imageView = (ImageView) findViewById(R.id.addJob_imageView);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
    
        }
    

2 个答案:

答案 0 :(得分:0)

您应该使用PHP或任何服务器脚本将图像从android上传到服务器。请尝试下面给出的网址

Upload Image to Server PHP Android

答案 1 :(得分:0)

这就是答案!

 ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    // Compress image to lower quality scale 1 - 100
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
                    byte[] imagez = stream.toByteArray();

                    ParseFile filez = new ParseFile("androidbegin.png",
                            imagez);

                    filez.saveInBackground();

                    imgupload = new ParseObject("Anunturi");

                    imgupload.put("JobPictureName", "AndroidBegin Logo");

                    imgupload.put("jobPicture", filez);

                    imgupload.put("jobPictureName", picturePath);
                    imgupload.put("username", mUsernameText);
                    imgupload.put("jobName", mJobNameText);
                    imgupload.put("jobDescription", mJobDescriptionText);
                    imgupload.put("jobPrice", mJobPriceText);
                    imgupload.saveInBackground(new SaveCallback() {
                        public void done(ParseException e) {
                            if (e == null) {
                                Toast.makeText(getApplicationContext(),
                                        "Job succesfully posted!",
                                        Toast.LENGTH_LONG).show();
                                Intent in = new Intent(
                                        getApplicationContext(),
                                        JobsListActivity.class);
                                in.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                startActivity(in);
                                finish();
                            } else {
                                mGoogleNow
                                        .setVisibility(mGoogleNow.INVISIBLE);
                                Toast.makeText(getApplicationContext(),
                                        "An error occured",
                                        Toast.LENGTH_LONG).show();
                            }
                        }

                    });
                } else {
                    bitmap = null;
                    imgupload = new ParseObject("Anunturi");
                    imgupload.put("username", mUsernameText);
                    imgupload.put("jobName", mJobNameText);
                    imgupload.put("jobDescription", mJobDescriptionText);
                    imgupload.put("jobPrice", mJobPriceText);
                    imgupload.put("jobPictureName", "null");
                    imgupload.saveInBackground(new SaveCallback() {
                        public void done(ParseException e) {
                            if (e == null) {
                                Toast.makeText(
                                        getApplicationContext(),
                                        "Job succesfully posted!",
                                        Toast.LENGTH_LONG).show();
                                Intent in = new Intent(
                                        getApplicationContext(),
                                        JobsListActivity.class);
                                in.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                startActivity(in);
                                finish();
                            } else {
                                Toast.makeText(
                                        getApplicationContext(),
                                        "Error while posting job...",
                                        Toast.LENGTH_LONG).show();
                            }
                        }

                    });