上传图片到Parse不起作用

时间:2015-08-06 05:49:33

标签: android image file-upload parse-platform image-uploading

我有一个"保存"将上传图像进行解析的按钮。它过去工作得很好。但由于某种原因,它停止工作。

当我按下按钮时,没有任何反应。好像代码忽略了onClick方法。我认为按钮或其他保存对象有问题。因此,当我禁用图片上传时,我禁用了每一个对象并发现它工作正常。

因此,如果我禁用图片上传,我仍然可以保存名称。但是当我启用图像上传时,没有任何反应,感觉就像忽略了onClick方法。

这很奇怪。我的代码中没有任何改变,现在它无法正常工作。它曾经工作过。

你们有什么想法吗?

这是包含日志的更新代码:

protected Button mSavePet;

@Override
protected void onCreate(Bundle savedInstanceState) {

    mSave = (Button) findViewById(R.id.Save);

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

            Log.d("Log", "save button clicked");
            myUploading();
        }
    });
}


private void myUploading() {
    Log.d("Log", "myUploadig Start");

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

    ParseUser currentUser = ParseUser.getCurrentUser();
    String currentUserUsername = currentUser.getUsername();

    //get the new pet info
    String petname = mPetName.getText().toString();

    Log.d("Log", "puting objects start");
        //Save Pet to the cloud
        final ParseObject petObject = new ParseObject("MyPets");
        petObject.put("petName", petname);
        petObject.put("user", currentUserUsername);

        // Create the ParseFile for Image
        final ParseFile file = new ParseFile("petImage.png", image);
        // Upload the image into Parse Cloud
        file.saveInBackground(new SaveCallback() {
            @Override
            public void done(ParseException e) {
                Log.d("Log", "SaveFileInBackground Start");
                if(e == null){

                    Log.d("Log", "Putting ImageFile Start");
                    // Create a column named "ImageFile" and insert the image
                    petObject.put("petImage", file);

                    // Create the class and the columns
                    petObject.saveInBackground(new SaveCallback() {
                        @Override
                        public void done(ParseException e) {
                            if (e == null) {
                                Log.d("Log", "putting objects finished");
                                finish();
                            } else {
                            }
                        }
                    });
                }
            }
        });

    }
}

这是代码输出:

08-06 20:04:56.117  29376-29376/com.example.stanleysantoso.wikipetia D/Log﹕ save button clicked
08-06 20:04:56.129  29376-29376/com.example.stanleysantoso.wikipetia D/Log﹕ myUploadig Start
08-06 20:05:08.008  29376-29376/com.example.stanleysantoso.wikipetia D/Log﹕ puting objects start
08-06 20:07:06.986  29376-29376/com.example.stanleysantoso.wikipetia D/Log﹕ save button clicked
08-06 20:07:06.986  29376-29376/com.example.stanleysantoso.wikipetia D/Log﹕ myUploadig Start
08-06 20:07:16.972  29376-29376/com.example.stanleysantoso.wikipetia D/Log﹕ puting objects start

所以,它从未进入file.saveInBackGround。

1 个答案:

答案 0 :(得分:0)

您尝试拨打file.saveInBackground();并在下一行Object.put("Image", file);。由于saveInBackground()是异步的,因此在您尝试将此文件对象放入Object后可能会完成。尝试添加回调并将此代码移至回调:

file.saveInBackground(new SaveCallback callback() {
    @Override()
    public void done(ParseException e) {
        if (e == null) {
            Object.put("Image", file);

            // Create the class and the columns
            petObject.saveInBackground(new SaveCallback() {
                @Override
                public void done(ParseException e) {
                    if (e == null) {
                        finish();
                    } else {

                    }
               }
            });
        } else {
        }
   }
});