6

ANDROID

This is how I store ParseFile List into ParseObject

ParseObject pObject = new ParseObject();    
ArrayList<ParseFile> pFileList = new ArrayList<ParseFile>();
    for (String thumbPath : thumbList) {
       byte[] imgData = convertFileToByteArray(thumbPath);
       ParseFile pFile = new ParseFile("mediaFiles",imgData);
       pFileList.add(pFile);    
    }

       pObject.addAll("mediaFiles", pFileList); 
       pObject.saveEventually();

after this call it does not show the inserted row in data browser, although it shows rowcount of 1 in table

This is how i retrieve it and get the first image from the list

List<ParseFile> pFileList = (ArrayList<ParseFile>) pObject.get("mediaFiles");
    if (!pFileList.isEmpty()) {
          ParseFile pFile = pFileList.get(0);
          byte[] bitmapdata = pFile.getData();  // here it throws error
          bitmap = BitmapFactory.decodeByteArray(bitmapdata, 0, bitmapdata.length);
    }

I am able to retrieve all String columns , but for "mediaFiles" column while doing getData() I get thisexception. com.parse.ParseException: Target host must not be null, or set in parameters.

I observed that in ParseFile, data and url is null.

Can somebody please show me the code on how to store and retrieve multiple ParseFile objects into single ParseObject?

4 Answers 4

3

Try uploading the ParseFiles using the save method before associating them with the Parse Object.

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

3 Comments

Will you help me fix this issue stackoverflow.com/a/24121701/1237175. Thanks
Can I have the code how the files are uploaded before associating to Parse Object. I am trying to do the same thing. Thnx
What if user cancel uploading the parse object? then the uploaded file will be orphan. It will be better to upload the parse object first, then the image.
0

Try to do this thing as upload image one after another,

   public void UploadImageToParse(String img_path, final String filedName, String Filename) {
        //String img_path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + "P200913_1908.jpg";

        final File file = new File(img_path);
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            Bitmap bitmap = BitmapFactory.decodeStream(fileInputStream);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            byte[] image_array = baos.toByteArray();
            final ParseFile parsefile = new ParseFile(Filename, image_array);
            parsefile.saveInBackground(new SaveCallback() {
                @Override
                public void done(ParseException e) {
                    if (e != null) {

                    } else {

                        frameInfo.put(filedName, parsefile);
                        frameInfo.saveInBackground(new SaveCallback() {
                            @Override
                            public void done(ParseException e) {
                                if (e == null) {
                                    DebugLog.e("" + e);

                                } else {
                                    fileUploadStatusUpdater.onFailure();
                                    DebugLog.e("File Upload Fail");
                                }
                            }
                        });
                        /*ParseUser user = ParseUser.getCurrentUser();
                        user.put(filedName, parsefile);
                        user.saveInBackground(new SaveCallback() {
                            @Override
                            public void done(ParseException e) {
                                Log.e("", "" + e);
                            }
                        });*/
                    }
                }

            }, new ProgressCallback() {
                @Override
                public void done(Integer integer) {
                    if (integer == 100) {
                        DebugLog.e("File Upload Completed");
                        fileUploadStatusUpdater.onSuccess();
                    }
                }
            });
        } catch (Exception e) {
            DebugLog.e("Fis" + e);
            fileUploadStatusUpdater.onFailure();
        }

3 Comments

I've got some questions: 1) catch without try? 2) what is "fileUploadStatusUpdater"? 3) you are not referring to the code in the question, so what is "parsefile"? and "frameInfo"?
Check now i have updated my code. 1) fileUploadStatusUpdater is my custom interface callback to handle success or failure of image uploader task 2) FrameInfo in my custom ParseObject class it's depends on your object.
thanks :) - I had your question on the review queue, and while I'm doing android development, I never ran into this problem before. So I can't tell whether this answer is "useful", but I hope the person who posted the question will take a look.
0

Cover the byte[] bitmapdata = pFile.getData(); line of code under try catch. It worked for me!

Comments

-1
final ParseFile parseFile1 = new ParseFile("poll_image1.jpg",scaleImage("poll_image1",imageList.get(contestImage1.getId())));
                        final ParseFile parseFile2 = new ParseFile("poll_image2.jpg",scaleImage("poll_image2",imageList.get(contestImage2.getId())));
                        parseFile1.save(); parseFile2.save();

                        List<ParseFile> listOfFiles = new ArrayList<ParseFile>();
                        listOfFiles.add(parseFile1);
                        listOfFiles.add(parseFile2);


                        ParseObject jobApplication = new ParseObject("Poll");
                        jobApplication.put("poll_question", contestQuestion.getText().toString());
                        jobApplication.put("poll_type_id", 1);
                        ParseUser currentUser = ParseUser.getCurrentUser();
                        jobApplication.put("user", currentUser);
                        jobApplication.put("parseFile", listOfFiles);
                        jobApplication.saveInBackground(new SaveCallback() {

                            @Override
                            public void done(ParseException arg0) {

                            }
                        });

Above code does multi upload, but ParseObject called only after Two save() method. Because of two save method UI getting Stuck. How to fix it!

1 Comment

This library(loopj.com/android-async-http) does Multipart file upload. I need to do similar using parse, Anybody can help me ?

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.