Timmy Simons
Timmy Simons

Reputation: 599

storing multiple ParseFile objects in single ParseObject

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?

Upvotes: 6

Views: 3485

Answers (4)

Kamya Batra
Kamya Batra

Reputation: 1

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

Upvotes: 0

Sanket Shah
Sanket Shah

Reputation: 1

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

Upvotes: 0

Ramesh_D
Ramesh_D

Reputation: 689

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!

Upvotes: -1

H&#233;ctor Ramos
H&#233;ctor Ramos

Reputation: 9258

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

Upvotes: 3

Related Questions