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 trialMUZ140698 Kudakwashe Gwaindepi
4,150 Pointscannot resolve symbol 'mediaType'
can someone assit me, when i try to set an if statement " if(mediaType == MEDIA_TYPE_IMAGE )" i get the error that says "cannot resolve mediaType"
also noticed that in the Uri getOutputMediaFileUri method, the parameter "mediaType" is grayed out
here is my code
public void onClick(DialogInterface dialog, int which) { switch (which){ case 0: //Take picture Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); String appName = MainActivity.this.getString(R.string.app_name); mMediaUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); if(mMediaUri==null){ Toast.makeText(MainActivity.this, R.string.error_external_storage, Toast.LENGTH_LONG).show(); } takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, mMediaUri); startActivityForResult(takePhotoIntent, TAKE_PHOTO_REQUEST);
//1. get the external storage directory
File mediaStorageDir = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), String.valueOf(R.string.appName));
//2. create our subdirectory
if(! mediaStorageDir.exists()){
if(! mediaStorageDir.mkdirs()){
Log.e(TAG, "Failed to create Directory.");
return;
}
}
//3.create a file name
File mediaFile;
Date now = new Date();
String timeStamp = new SimpleDateFormat("yyyyMMdd _HHmmss", Locale.US).format(now);
String path = mediaStorageDir.getPath()+ File.separator;
if(mediaType == MEDIA_TYPE_IMAGE ){
mediaFile= new File(path +"IMG_" + timeStamp + ".jpg");
}
else if (mediaType == MEDIA_TYPE_VIDEO){
mediaFile = new File(path + "VID_" + timeStamp + ".mp4");
}
else {
return;
}
Log.d(TAG, "File" + Uri.fromFile(mediaFile));
//5.return file URL
return; Uri.fromFile(mediaFile);
case 1: //Take Video
break;
case 2://chose picture
break;
case 3:
//chose video
break;
}
}
private Uri getOutputMediaFileUri(int mediaType) {
if(isExternalStorageAvailable()){
//get Uri
return null;
}
else {
return null;
}
}
private boolean isExternalStorageAvailable(){
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)){
return true;
}
else {
return false;
}
}
};
4 Answers
David Postlethwaite
9,355 PointsThe code you gave looks incomplete, Do you have :
private Uri getOutputMediaFileUri(int mediaType) {
at the start of the method? If the compiler says it cannot resolve it normally means the variable has not been declared
MUZ140698 Kudakwashe Gwaindepi
4,150 Pointsyes i do, its right there , above the isExternalStorageAvailable method
like this:
private Uri getOutputMediaFileUri(int mediaType) { if(isExternalStorageAvailable()){ //get Uri return null; } else { return null; }
David Postlethwaite
9,355 PointsLooks like you have placed the code for the getOutputMediaFileUri(int mediaType) method in the wrong place. you have placed it in the OnClick method of the OnClickListener or just after it. Watch the video again and watch where he places the code. Just paste and cut into the if(isExternalStorageAvailable()) statement replacing the //get Uri return null;.
if you need any more help just let me know.
Vrezh Gulyan
11,161 PointsSteps 1 through 5 that we are doing in this video belong in the getOutputMedialFileUri method that just bellow the Dialog Interface on click listener. You just have the code in the wrong place.
private Uri getOutputMediaFileUri(int mediaType) {
if(isExternalStorageAvailable()){
//get Uri
//1. get external storage
//2. create sub-directory
//3. create a file name
//4. create file
//5. return the files uri
return null;
}
else {
return null;
}
Just copy all the code you have for steps 1 through 5 into where I showed you in the above could in the if (isExternalStorageAvailable()) code block. Your error will go away because in this method mediaType is passed in as a parameter.