Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Build a Self-Destructing Message Android App Capturing Photos and Videos Taking a Photo Using an Intent

Luke Liem
Luke Liem
6,367 Points

cannot find symbol variable mediaStore

I get two errors when compiling the code:

C:\Users\Luke\AStudio082Projects\Rabbit\app\src\main\java\com\scipio\luke\rabbit\MainActivity.java:51: error: cannot find symbol Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); ^ symbol: variable MediaStore Note: C:\Users\Luke\AStudio082Projects\Rabbit\app\src\main\java\com\scipio\luke\rabbit\MainActivity.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. Note: C:\Users\Luke\AStudio082Projects\Rabbit\app\src\main\java\com\scipio\luke\rabbit\FriendsFragment.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. 1 error

FAILED

FAILURE: Build failed with an exception.

  • What went wrong: Execution failed for task ':app:compileDebugJava'.

    Compilation failed; see the compiler error output for details.

  • Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 7.555 secs

My MainActivity Code:

C:\Users\Luke\AStudio082Projects\Rabbit\app\src\main\java\com\scipio\luke\rabbit\MainActivity.java:51: error: cannot find symbol Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); ^ symbol: variable MediaStore Note: C:\Users\Luke\AStudio082Projects\Rabbit\app\src\main\java\com\scipio\luke\rabbit\MainActivity.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. Note: C:\Users\Luke\AStudio082Projects\Rabbit\app\src\main\java\com\scipio\luke\rabbit\FriendsFragment.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. 1 error

FAILED

FAILURE: Build failed with an exception.

  • What went wrong: Execution failed for task ':app:compileDebugJava'.

    Compilation failed; see the compiler error output for details.

  • Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 7.555 secs

Luke Liem
Luke Liem
6,367 Points

My Code:

package com.scipio.luke.rabbit;

import android.app.ActionBar; import android.app.AlertDialog; import android.app.FragmentTransaction; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.support.v13.app.FragmentPagerAdapter; import android.support.v4.app.FragmentActivity; import android.support.v4.view.ViewPager; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.Window;

import com.parse.ParseUser;

// MainActivity is extending FragmentActivity instead of Activity // It is also implementing the ActionBar.TabListener interface public class MainActivity extends FragmentActivity implements ActionBar.TabListener {

/**
 * The {@link android.support.v4.view.PagerAdapter} that will provide
 * fragments for each of the sections. We use a
 * {@link FragmentPagerAdapter} derivative, which will keep every
 * loaded fragment in memory. If this becomes too memory intensive, it
 * may be best to switch to a
 * {@link android.support.v13.app.FragmentStatePagerAdapter}.
 */
SectionsPagerAdapter mSectionsPagerAdapter;

/**
 * The {@link ViewPager} that will host the section contents.
 */
ViewPager mViewPager;
public static final String TAG = MainActivity.class.getSimpleName();

public final static int TAKE_PHOTO_REQUEST = 0;
public final static int TAKE_VIDEO_REQUEST = 1;
public final static int CHOOSE_PHOTO_REQUEST = 2;
public final static int CHOOSE_VIDEO_REQUEST = 3;

// Implement OnClick Listener for User Clicking on an Item in the Dialog List
protected DialogInterface.OnClickListener mDialogListener = new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        switch (which){
            case 0: //Take Picture
                // create Intent to take a picture and return control to the calling application
                Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                // start the image capture Intent
                startActivityForResult(takePhotoIntent, TAKE_PHOTO_REQUEST);
                break;
            case 1: //Take Video
                break;
            case 2: //Choose Picture
                break;
            case 3: //Choose Picture
                break;
        }
    }
};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // This allows progress bar to be called in Fragments (Inbox and Friends)
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setContentView(R.layout.activity_main);

    // Whenever you use any signup or login methods, the user is cached on disk. By using the
    // cached currentUser object, the user does not have to login every time he opens the app
    ParseUser currentUser = ParseUser.getCurrentUser();
    if (currentUser == null) {
        navigateToLogin();
    }
    else {
        Log.i(TAG,currentUser.getUsername());
    }


    // Set up the action bar.
    final ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Create the adapter that will return a fragment for each of the
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    // When swiping between different sections, select the corresponding
    // tab. We can also use ActionBar.Tab#select() to do this if we have
    // a reference to the Tab.
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }
    });

    // For each of the sections in the app, add a tab to the action bar.
    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
        // Create a tab with text corresponding to the page title defined by
        // the adapter. Also specify this Activity object, which implements
        // the TabListener interface, as the callback (listener) for when
        // this tab is selected.
        actionBar.addTab(
                actionBar.newTab()
                        .setText(mSectionsPagerAdapter.getPageTitle(i))
                        .setTabListener(this));
    }
}

private void navigateToLogin() {
    // The user is directed to Login Page
    Intent intent = new Intent(this, LoginActivity.class);

    // This removes MainActivity and set the LoginActivity at the bottom of the Stack
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);

    startActivity(intent);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    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_logout:   // User wants to log out
        ParseUser.logOut();
        navigateToLogin();
        return true;

        case R.id.action_edit_friends:   //User wants to Edit Friends List
        Intent intent = new Intent(this, EditFriendsActivity.class);
        startActivity(intent);

        case R.id.action_camera:    //User wants to add picture or video
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setItems(R.array.camera_choices,mDialogListener);
            AlertDialog dialog = builder.create();
            dialog.show();
    }
    return super.onOptionsItemSelected(item);
}

// These methods are associated with ActionBar.TabListener
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    // When the given tab is selected, switch to the corresponding page in
    // the ViewPager.
    mViewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}

@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}

}

1 Answer

Luke Liem
Luke Liem
6,367 Points

I was able to fix the code by adding the following import statement to MainActivity.java:

import android.provider.MediaStore;

I am using the Android Studio 0.8.6. and it appears that this beta version of AS does not autoimport android.provider.MediaStore when you only have:

            Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(takePhotoIntent, TAKE_PHOTO_REQUEST);

Though for some strange reason, if you were to have:

                Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
               mMediaUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
                if (mMediaUri == null) {
                    Toast.makeText(MyActivity.this, R.string.error_external_storage, Toast.LENGTH_LONG).show();
                }
                takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, mMediaUri);
                startActivityForResult(takePhotoIntent, TAKE_PHOTO_REQUEST);

Then it will autoimport android.provider.MediaStore.