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

Failed to send recorded video! Please help!

I've come to the "Sending the message" video in the self-destructing app project, but when I try to send a captured video, the app closes and says "Unfortunately, Lecture (The name of my app) has stopped". I searched up in the logcat, but I can't find the error. I've replaced parts of the code with the ones in the project files download, but I still get the error. This is the logcat error I'm getting:

02-10 21:31:31.201: I/MainActivity(1857): Media URI: content://media/external/images/media/1997
02-10 21:31:33.463: D/dalvikvm(1857): GC_FOR_ALLOC freed 1435K, 39% free 6938K/11203K, paused 28ms, total 28ms
02-10 21:31:33.483: D/dalvikvm(1857): GC_FOR_ALLOC freed 30K, 30% free 7931K/11203K, paused 22ms, total 22ms
02-10 21:31:33.513: D/dalvikvm(1857): GC_FOR_ALLOC freed 38K, 19% free 9151K/11203K, paused 21ms, total 21ms
02-10 21:31:33.523: I/dalvikvm-heap(1857): Grow heap (frag case) to 16.904MB for 644296-byte allocation
02-10 21:31:33.563: D/dalvikvm(1857): GC_FOR_ALLOC freed 2599K, 40% free 7183K/11847K, paused 25ms, total 25ms
02-10 21:31:33.663: I/dalvikvm-heap(1857): Grow heap (frag case) to 28.430MB for 14745616-byte allocation
02-10 21:31:33.693: D/dalvikvm(1857): GC_CONCURRENT freed <1K, 18% free 21583K/26311K, paused 2ms+3ms, total 30ms
02-10 21:31:33.833: D/dalvikvm(1857): GC_FOR_ALLOC freed <1K, 18% free 21584K/26311K, paused 24ms, total 24ms
02-10 21:31:33.953: I/dalvikvm-heap(1857): Grow heap (frag case) to 39.543MB for 11653136-byte allocation
02-10 21:31:33.983: D/dalvikvm(1857): GC_CONCURRENT freed <1K, 13% free 32964K/37703K, paused 9ms+4ms, total 33ms
02-10 21:31:41.471: D/dalvikvm(1857): GC_FOR_ALLOC freed 16380K, 49% free 20611K/39751K, paused 21ms, total 21ms
02-10 21:31:45.425: D/dalvikvm(1857): GC_FOR_ALLOC freed 2047K, 44% free 22658K/39751K, paused 20ms, total 20ms
02-10 21:32:01.380: I/MainActivity(1857): Media URI: content://media/external/video/media/1979
02-10 21:32:38.987: D/dalvikvm(1857): GC_FOR_ALLOC freed 16977K, 73% free 11089K/39751K, paused 28ms, total 28ms
02-10 21:32:38.997: D/skia(1857): --- SkImageDecoder::Factory returned null
02-10 21:32:38.997: D/skia(1857): --- SkImageDecoder::Factory returned null
02-10 21:32:38.997: D/AndroidRuntime(1857): Shutting down VM
02-10 21:32:38.997: W/dalvikvm(1857): threadid=1: thread exiting with uncaught exception (group=0x41683318)
02-10 21:32:39.027: E/AndroidRuntime(1857): FATAL EXCEPTION: main
02-10 21:32:39.027: E/AndroidRuntime(1857): java.lang.NullPointerException
02-10 21:32:39.027: E/AndroidRuntime(1857):     at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:461)
02-10 21:32:39.027: E/AndroidRuntime(1857):     at com.larsfso.lecture.ImageResizer.resizeImage(ImageResizer.java:27)
02-10 21:32:39.027: E/AndroidRuntime(1857):     at com.larsfso.lecture.ImageResizer.resizeImageMaintainAspectRatio(ImageResizer.java:55)
02-10 21:32:39.027: E/AndroidRuntime(1857):     at com.larsfso.lecture.FileHelper.reduceImageForUpload(FileHelper.java:67)
02-10 21:32:39.027: E/AndroidRuntime(1857):     at com.larsfso.lecture.RecipientsActivity.createMessage(RecipientsActivity.java:174)
02-10 21:32:39.027: E/AndroidRuntime(1857):     at com.larsfso.lecture.RecipientsActivity.onOptionsItemSelected(RecipientsActivity.java:129)
02-10 21:32:39.027: E/AndroidRuntime(1857):     at android.app.Activity.onMenuItemSelected(Activity.java:2534)
02-10 21:32:39.027: E/AndroidRuntime(1857):     at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:965)
02-10 21:32:39.027: E/AndroidRuntime(1857):     at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
02-10 21:32:39.027: E/AndroidRuntime(1857):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
02-10 21:32:39.027: E/AndroidRuntime(1857):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
02-10 21:32:39.027: E/AndroidRuntime(1857):     at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:514)
02-10 21:32:39.027: E/AndroidRuntime(1857):     at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:99)
02-10 21:32:39.027: E/AndroidRuntime(1857):     at android.view.View.performClick(View.java:4103)
02-10 21:32:39.027: E/AndroidRuntime(1857):     at android.view.View$PerformClick.run(View.java:17117)
02-10 21:32:39.027: E/AndroidRuntime(1857):     at android.os.Handler.handleCallback(Handler.java:615)
02-10 21:32:39.027: E/AndroidRuntime(1857):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-10 21:32:39.027: E/AndroidRuntime(1857):     at android.os.Looper.loop(Looper.java:137)
02-10 21:32:39.027: E/AndroidRuntime(1857):     at android.app.ActivityThread.main(ActivityThread.java:4744)
02-10 21:32:39.027: E/AndroidRuntime(1857):     at java.lang.reflect.Method.invokeNative(Native Method)
02-10 21:32:39.027: E/AndroidRuntime(1857):     at java.lang.reflect.Method.invoke(Method.java:511)
02-10 21:32:39.027: E/AndroidRuntime(1857):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-10 21:32:39.027: E/AndroidRuntime(1857):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-10 21:32:39.027: E/AndroidRuntime(1857):     at dalvik.system.NativeStart.main(Native Method)
02-10 21:33:00.698: I/Process(1857): Sending signal. PID: 1857 SIG: 9

I also have a delay with the "Message Sent" when pressing the "send" button, but I would assume this occurs after it's successfully uploaded to parse. There's also a delay when pressing "send" after selecting recipients to a "galleryphoto". The delay is about 15 seconds long. But I don't care about the delay that much.

Thanks in advance! :D

Tagging Ben Jakuben on this one... ^_^

2 Answers

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Based on those error messages, it looks like your code is attempting to handle the video as an image (it shouldn't be using the ImageResizer class for a video. Can you debug or figure out what the mFileType value is in your send() method? Perhaps that is getting the wrong value somehow.

If you continue to have trouble, can you paste in your whole RecipientsActivity code?

I will see if I can fix the problem now. But here's the whole RecipientsActivity:

package com.larsfso.lecture;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
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;

public class RecipientsActivity extends ListActivity {

    public static final String TAG = RecipientsActivity.class.getSimpleName();

    protected ParseRelation<ParseUser> mFriendsRelation;
    protected ParseUser mCurrentUser;   
    protected List<ParseUser> mFriends; 
    protected MenuItem mSendMenuItem;
    protected Uri mMediaUri;
    protected String mFileType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.activity_recipients);
        // Show the Up button in the action bar.
        setupActionBar();

        getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

        mMediaUri = getIntent().getData();
        mFileType = getIntent().getExtras().getString(ParseConstants.KEY_FILE_TYPE);
    }

    @Override
    public void onResume() {
        super.onResume();

        mCurrentUser = ParseUser.getCurrentUser();
        mFriendsRelation = mCurrentUser.getRelation(ParseConstants.KEY_FRIENDS_REALTION);

        setProgressBarIndeterminateVisibility(true);

        ParseQuery<ParseUser> query = mFriendsRelation.getQuery();
        query.addAscendingOrder(ParseConstants.KEY_USERNAME);
        query.findInBackground(new FindCallback<ParseUser>() {
            @Override
            public void done(List<ParseUser> friends, ParseException e) {
                setProgressBarIndeterminateVisibility(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);
                    setListAdapter(adapter);
                }
                else {
                    Log.e(TAG, e.getMessage());
                    AlertDialog.Builder builder = new AlertDialog.Builder(RecipientsActivity.this);
                    builder.setMessage(e.getMessage())
                        .setTitle(R.string.error_title)
                        .setPositiveButton(android.R.string.ok, null);
                    AlertDialog dialog = builder.create();
                    dialog.show();
                }
            }
        });
    }

    /**
     * Set up the {@link android.app.ActionBar}.
     */
    private void setupActionBar() {

        getActionBar().setDisplayHomeAsUpEnabled(true);

    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            // This ID represents the Home or Up button. In the case of this
            // activity, the Up button is shown. Use NavUtils to allow users
            // to navigate up one level in the application structure. For
            // more details, see the Navigation pattern on Android Design:
            //
            // http://developer.android.com/design/patterns/navigation.html#up-vs-back
            //
            NavUtils.navigateUpFromSameTask(this);
            return true;
        case R.id.action_send:
            ParseObject message = createMessage();
            if (message == null) {
                // error
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage(R.string.error_selecting_file)
                    .setTitle(R.string.error_selecting_file_title)
                    .setPositiveButton(android.R.string.ok, null);
                AlertDialog dialog = builder.create();
                dialog.show();
            }
            else {
                send(message);
                finish();
            }
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        if (l.getCheckedItemCount() > 0) {
            mSendMenuItem.setVisible(true);
        }
        else {
            mSendMenuItem.setVisible(false);
        }
    }

    protected ParseObject createMessage() {
        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_RECIPIENTS_IDS, getRecipientIds());
        message.put(ParseConstants.KEY_FILE_TYPE, mFileType);

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

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

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

            return message;
        }
    }

    protected ArrayList<String> getRecipientIds() {
        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, R.string.success_message, Toast.LENGTH_LONG).show();
                }
                else {
                    AlertDialog.Builder builder = new AlertDialog.Builder(RecipientsActivity.this);
                    builder.setMessage(R.string.error_sending_message)
                        .setTitle(R.string.error_selecting_file_title)
                        .setPositiveButton(android.R.string.ok, null);
                    AlertDialog dialog = builder.create();
                    dialog.show();
                }
            }
        });
    }
}

Ben Jakuben

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

This line in your log output:

02-10 21:32:39.027: E/AndroidRuntime(1857): at com.larsfso.lecture.FileHelper.reduceImageForUpload(FileHelper.java:67)

Means that your code is attempting to call reduceImageForUpload(), which only happens in this block:

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

The only way you get in there is if mFileType is TYPE_IMAGE, so it does seem like that's the problem somewhere. mFileType comes from the Intent extras, so you'll need to go back to MainActivity to see how it's being set in the onActivityResult() method. It should look like this:

String fileType;
if (requestCode == PICK_PHOTO_REQUEST || requestCode == TAKE_PHOTO_REQUEST) {
    fileType = ParseConstants.TYPE_IMAGE;
}
else {
    fileType = ParseConstants.TYPE_VIDEO;
}

recipientsIntent.putExtra(ParseConstants.KEY_FILE_TYPE, fileType);

Thank you so much, @BenJakuben! You're the man! I sorted it out with your help. I had this line of code:

if (requestCode == PICK_PHOTO_REQUEST || requestCode == PICK_VIDEO_REQUEST) {

Thanks for your help! You're awesome! ^_^ <3

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Hooray! Onward and upward! :)