我无法弄清楚为什么会这样。这可能是一个我无法辨认的愚蠢错误。错误再次出现:
getOutputMediaFile(int) is undefined for the type new Camera.PictureCallback(){}
我的代码:
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());
}
}
};
答案 0 :(得分:34)
如果您因为相机示例到达此处,请向下滚动到文档中,该方法最后写入,因为视频和图像捕获都很常见 http://developer.android.com/guide/topics/media/camera.html#saving-media
答案 1 :(得分:1)
请参阅相机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");
}
}
}
}
修改强>
尝试importing android.provider.MediaStore.Files.FileColumns
并更改MEDIA_TYPE_IMAGE to FileColumns.MEDIA_TYPE_IMAGE
。
如果您正在调用相机
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);