Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Can't upload a video of more than 4s on parse.com

Hi guys, for some reason, when my video is more than 4s long, my application says an error occurs, below is my recipientActitivity.java and ans the error i have. The error says ParseFile must be less than 10485760 bytes. Please help me

package com.paveynganpi.snapshare;

import android.app.AlertDialog;
import android.net.Uri;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseFile;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseRelation;
import com.parse.ParseUser;
import com.parse.SaveCallback;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


public class RecipientsActivity extends ActionBarActivity {

    private static final String TAG = RecipientsActivity.class.getSimpleName();
    protected List<ParseUser> mFriends;
    protected ListView mListView;
    protected ParseRelation<ParseUser> mFriendsRelation;
    protected ParseUser mCurrentUser;
    protected MenuItem mSendMenuItem;
    protected Uri mMedialUri;
    protected String mFileType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recipients);
        getListView().setChoiceMode(mListView.CHOICE_MODE_MULTIPLE);

        //get the uri data from the MainActivity
        mMedialUri = getIntent().getData();
        mFileType = getIntent().getExtras().getString(ParseConstants.KEY_FILE_TYPE);

        //when an item on the list is clicked, set the button to be visible
        getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                int count = getListView().getCheckedItemCount();
                if(count>=1){
                    Log.d(TAG,"count "+count);
                    mSendMenuItem.setVisible(true);
                }
                else{
                    mSendMenuItem.setVisible(false);
                }
            }
        });

    }

    @Override
    public void onResume() {

        super.onResume();

        //supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        mCurrentUser = ParseUser.getCurrentUser();
        mFriendsRelation = mCurrentUser.getRelation(ParseConstants.KEY_FRIENDS_RELATION);
        ParseQuery<ParseUser> query = mFriendsRelation.getQuery();



        //setSupportProgressBarIndeterminate(true);
        query.orderByAscending(ParseConstants.KEY_USERNAME);
        query.findInBackground(new FindCallback<ParseUser>() {
            @Override
            public void done(List<ParseUser> friends, ParseException e) {
                //setSupportProgressBarIndeterminate(false);
                if(e == null){

                    mFriends = friends;

                    String[] usernames = new String[mFriends.size()];
                    int i = 0;
                    for (ParseUser user : mFriends) {

                        usernames[i] = user.getUsername();
                        i++;

                    }

                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                            getListView().getContext(),
                            android.R.layout.simple_list_item_checked,
                            usernames
                    );
                    //get reference to the list view

                    getListView().setAdapter(adapter);
                    //setListAdapter(adapter);

                }
                else{

                    Log.e(TAG, e.getMessage().toString());
                    AlertDialog.Builder builder = new AlertDialog.Builder(RecipientsActivity.this);
                    builder.setMessage(e.getMessage());//creates a dialog with this message
                    builder.setTitle(R.string.error_title);
                    builder.setPositiveButton(android.R.string.ok, null);//creates a button to dismiss the dialog

                    AlertDialog dialog = builder.create();//create a dialog
                    dialog.show();//show the dialog

                }

            }
        });

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_recipients, menu);
        mSendMenuItem = menu.getItem(0);//get the menu Object at position 0;
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        switch (id) {
            case R.id.action_send:
                ParseObject message = createMessages();

                if(message == null){
                    //error
                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setMessage(getString(R.string.error_selecting_file))
                            .setTitle(getString(R.string.error_selecting_file_title));
                    AlertDialog dialog = builder.create();
                    dialog.show();

                }
                else {
                    send(message);
                    finish();//move out of this activity, and next in stack comes up
                }
                return true;
            case R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;

        }
        return super.onOptionsItemSelected(item);
    }

    //gets an instance of the listview, returns a list view which is used to be able to call
    // getListView().setChoiceMode(mListView.CHOICE_MODE_MULTIPLE);
    protected ListView getListView() {
        if (mListView == null) {
            mListView = (ListView) findViewById(android.R.id.list);
        }
        return mListView;
    }

    protected ParseObject createMessages(){

        ParseObject message = new ParseObject(ParseConstants.CLASS_MESSAGES);
        message.put(ParseConstants.KEY_SENDER_ID, ParseUser.getCurrentUser().getObjectId());
        message.put(ParseConstants.KEY_SENDER_NAME,ParseUser.getCurrentUser().getUsername());
        message.put(ParseConstants.KEY_RECIPIENT_IDS,getRecipientId());
        message.put(ParseConstants.KEY_FILE_TYPE,mFileType);

        byte[] fileBytes = FileHelper.getByteArrayFromFile(this,mMedialUri);

        if(fileBytes == null){
            return null;
        }
        else {
            if (mFileType.equals(ParseConstants.TYPE_IMAGE)) {
                fileBytes = FileHelper.reduceImageForUpload(fileBytes);

            }

            String fileName = FileHelper.getFileName(this,mMedialUri,mFileType);
            ParseFile file = new ParseFile(fileName,fileBytes);
            message.put(ParseConstants.KEY_FILE,file);
            return message;
        }

    }

    protected ArrayList<String> getRecipientId(){

        ArrayList<String> recipientIds = new ArrayList<String>();

        for(int i =0; i< getListView().getCount();i++){

            if(getListView().isItemChecked(i)){
                recipientIds.add(mFriends.get(i).getObjectId());
            }

        }
        return recipientIds;

    }

    protected void send(ParseObject message){

        message.saveInBackground(new SaveCallback() {
            @Override
            public void done(ParseException e) {

                if( e == null){
                    //success
                    Toast.makeText(RecipientsActivity.this,getString(R.string.success_message),Toast.LENGTH_LONG).show();

                }
                else{

                    AlertDialog.Builder builder = new AlertDialog.Builder(RecipientsActivity.this);
                    builder.setMessage(getString(R.string.error_sending_message))
                            .setTitle(getString(R.string.error_selecting_file_title))
                            .setPositiveButton(android.R.string.ok,null);
                    AlertDialog dialog = builder.create();
                    dialog.show();


                }


            }
        });

    }

}

below is the error

01-03 21:10:16.611    4395-4395/com.paveynganpi.snapshare E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.paveynganpi.snapshare, PID: 4395
    java.lang.IllegalArgumentException: ParseFile must be less than 10485760 bytes
            at com.parse.ParseFile.<init>(ParseFile.java:73)
            at com.parse.ParseFile.<init>(ParseFile.java:106)
            at com.paveynganpi.snapshare.RecipientsActivity.createMessages(RecipientsActivity.java:203)
            at com.paveynganpi.snapshare.RecipientsActivity.onOptionsItemSelected(RecipientsActivity.java:150)
            at android.app.Activity.onMenuItemSelected(Activity.java:2708)
            at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:350)
            at android.support.v7.app.ActionBarActivity.onMenuItemSelected(ActionBarActivity.java:155)
            at android.support.v7.app.ActionBarActivityDelegate$1.onMenuItemSelected(ActionBarActivityDelegate.java:74)
            at android.support.v7.app.ActionBarActivityDelegateBase.onMenuItemSelected(ActionBarActivityDelegateBase.java:556)
            at android.support.v7.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:802)
            at android.support.v7.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:153)
            at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:949)
            at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:939)
            at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:596)
            at android.support.v7.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:145)
            at android.view.View.performClick(View.java:4640)
            at android.view.View$PerformClick.run(View.java:19421)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5579)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
            at dalvik.system.NativeStart.main(Native Method)

(RecipientsActivity.java:203) is

ParseFile file = new ParseFile(fileName,fileBytes);

and (RecipientsActivity.java:150) is

  ParseObject message = createMessages();

Thanks

1 Answer

I haven't taken any of the Android courses on Treehouse, but according to this article on IBM's developerWorks, there appears to a file size cap of 10 MB.

Under the Working with files section of the article:

File data is represented by the byte[] syntax. Note that currently, a given file cannot be larger than 10MB.

You'll need to decrease the file size of your video.

Thanks imouto , I am just surprised the professor's video went above 5s. Thanks once more.