Java getOutputMediaFile(int) 对于 new Camera.PictureCallback(){} 类型未定义,错误让我困惑
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23072666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getOutputMediaFile(int) is undefined for the type new Camera.PictureCallback(){} error confusing me
提问by CodeLover
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(){}
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());
}
}
};
回答by Mightian
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
如果您因为相机示例而到达这里,请进一步向下滚动到文档中,该方法写在最后,因为它对于视频和图像捕获都是通用的 http://developer.android.com/guide/topics/media/camera。 html#保存媒体
回答by Jawad Zeb
See Camera Android Code
查看相机 Android 代码
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
.
尝试importing android.provider.MediaStore.Files.FileColumns
和改变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);