I cannot figure out why this is occurring. It is probably a silly mistake that I cannot identify. Again the error is:
getOutputMediaFile(int) is undefined for the type new Camera.PictureCallback(){}
my code:
public static final int MEDIA_TYPE_IMAGE = 1;
private PictureCallback mPicture = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
//Log.d(TAG, "File not found: " + e.getMessage());
} catch (IOException e) {
// Log.d(TAG, "Error accessing file: " + e.getMessage());
}
}
};
The method getResources() is undefined for the type The method getSystemService(String) is undefined for the type List Problem This error message appears in java when the method is not defined in that class.
This tutorial demonstrates Java’s the method is undefined for the type error. The error the method is undefined for the type occurs whenever we try to call a method that is not defined in the current class. An example throws the the method is undefined for the type error.
The method (methodName) undefined for the type (className) Specific errors The method contains(String) is undefined for the type String The method codePointAt(String) is undefined for the type String
If you arrived here because of the camera example scroll down further into the documentation the method is written at the last as it's common to both video and the image capture http://developer.android.com/guide/topics/media/camera.html#saving-media
See Camera Android Code
public class MainActivity extends Activity {
public static final int MEDIA_TYPE_IMAGE = 1;
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
Uri fileUri ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt=(Button)findViewById(R.id.button1);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri= getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
});
}
private static Uri getOutputMediaFileUri(int type){
return Uri.fromFile(getOutputMediaFile(type));
}
@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;
}
private static File getOutputMediaFile(int type){
File mediaStorageDir = new File(Environment.getExternalStorageDirectory(), "MyCameraApp");
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE){
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
} else {
return null;
}
return mediaFile;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
TextView tv=(TextView)findViewById(R.id.textView2);
if (resultCode == RESULT_OK) {
tv.setText("Image saved to:\n"+data.getData());
ImageView img=(ImageView)findViewById(R.id.imageView1);
img.setImageURI(fileUri);
//tv.setText(fileUri.toString());
} else if (resultCode == RESULT_CANCELED) {
tv.setText("Cancelled");
} else {
// Image capture failed, advise user
tv.setText("Can con be captured");
}
}
}
}
Edit
Try importing android.provider.MediaStore.Files.FileColumns
and change MEDIA_TYPE_IMAGE to FileColumns.MEDIA_TYPE_IMAGE
.
if you are calling Camera like
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
// create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With