【发布时间】:2012-01-27 22:54:42
【问题描述】:
我有一个混合应用程序,可以打开安卓的原生相机。 我的代码:
public class CameraFunActivity extends Activity {
OpenCamera openCamera;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView webView=(WebView)findView ById(R.id.webkitWebView1);
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setDatabaseEnabled(true);
openCamera=new OpenCamera(webView,CameraFunActivity.this);
webView.addJavascriptInterface(openCamera,"camera");
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode== 0 && resultCode == Activity.RESULT_OK){
System.out.println("!!!!!!!!!!!!!!!!!!!!Camera Working...........");
String imagePath = "file:/"+openCamera.getPath();
System.out.println("Image Pathhhhhhhhhh :::::::::::: " + imagePath);
openCamera.setPath(imagePath);
.. }
HTML: 函数 captureImage1(){ camera.startCamera(); var path="file://" + camera.getPath(); //警报(路径); document.getElementById("image1").src=path; }
public OpenCamera (WebView appView,Activity context) {
this.mAppView = appView;
this.context=上下文;
}
public void setPath(String path){
_path=path;
}
public String getPath(){
return _path;
}
public void setBitmap(Bitmap bitmap){
System.out.println("setting bitmap");
this.bitmap=bitmap;
}
public Bitmap getBitmap(){
System.out.println("getting bitmap");
return bitmap;
}
public void startCamera(){
/*
Camera camera = Camera.open();
Camera.Parameters parameters = camera.getParameters();
parameters.setPictureFormat(PixelFormat.JPEG);
camera.setParameters(parameters);*/
Date dt = new Date();
int date=dt.getDate();
int hours = dt.getHours();
int minutes = dt.getMinutes();
int seconds = dt.getSeconds();
String curTime = date+"_"+hours + "_"+minutes + "_"+ seconds;
_path=Environment.getExternalStorageDirectory() +"/"+curTime+".jpg";
File file = new File( _path );
Uri outputFileUri = Uri.fromFile( file );
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
intent.putExtra( MediaStore.EXTRA_OUTPUT, outputFileUri );
// System.out.println("Paramssssssssssssss " + camera.getParameters().toString());
context.startActivityForResult(intent,0);
有时它会在 img 标签 src 处显示图像,有时则不会。需要帮助找出问题。
另外,它与方向变化有关吗?
谢谢
【问题讨论】:
标签: javascript android html camera