相机的奇怪行为

时间:2011-12-20 06:40:44

标签: java android html5

我正在使用android,js和html5开发混合应用程序。

我的代码如下:

java:

 public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  

        WebView webView=(WebView)findViewById(R.id.webkitWebView1);  
        WebSettings settings = webView.getSettings();  
        settings.setJavaScriptEnabled(true);  
        settings.setDatabaseEnabled(true);  
        settings.setDomStorageEnabled(true);  
        //To display the alert  
        webView.setWebViewClient(new WebViewClient());  
       webView.setWebChromeClient(new WebChromeClient());  
       webView.setWebChromeClient(new WebChromeClient() {  
             public void onExceededDatabaseQuota(String url, String databaseIdentifier, long currentQuota, long estimatedSize,
                long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {   
                quotaUpdater.updateQuota(estimatedSize * 1);
        }  
    });  

    //load  html5 page

        webView.loadUrl("file:///android_asset/html/index.html");  

       openCamera=new OpenCamera(webView,AvivaInsureActivity.this);  
        webView.addJavascriptInterface(openCamera,"camera"); 
  }

public class OpenCamera  {  
 private WebView mAppView;  
    private Activity context;  
    protected String _path;  
    private Bitmap bitmap;  

 public String getImagePath1() {  
        return imagePath1;
  }  

   public void setImagePath1(String imagePath1) { 
    this.imagePath1 = imagePath1; 
  }  

       public String getImagePath2() { 
        return imagePath2; 
  }

       public void setImagePath2(String imagePath2) { 
        this.imagePath2 = imagePath2; 
    }
     public void startCameraActivity1(){

       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;  
           imagePath1=Environment.getExternalStorageDirectory() +"/"+curTime+".jpg";  
       File file1 = new File(imagePath1);  


       Uri outputFileUri1 = Uri.fromFile( file1 );  


        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
    intent.putExtra( MediaStore.EXTRA_OUTPUT, outputFileUri1 );  

      context.startActivityForResult(intent,0);

  }

类似的camer2方法。 和js和html5:

<script type="text/javascript"><!--
function openCamera1(){ 
    camera.startCameraActivity1();  
    var path1=camera.getImagePath1();  
   //alert(path1);  
   document.getElementById("image1").src=path1;  
 }

html:

         
     
<div><input type="submit" value="Submit" onclick="submitForm()" /><br />
</div>

<div><img id="image1" height="50" width="50" />" "<img id="image2"
height="50" width="50" /></div>  

@Override   
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
       //data.getExtras();
         if (requestCode== 0 && resultCode == Activity.RESULT_OK){   
            String imagePath1 =  "file://"+openCamera.getImagePath1();  
            System.out.println("Image Pathhhhhhhhhh11111111 :::::::::::: " + imagePath1);   

... }

这里“数据”为空!!这怎么可能?我在这做什么是对的?

问题是,如果我评论警报(path1)和警报(path2),我无法在img标签上加载图像。它显示“?”代替。启用警报或尝试3-4次捕获图像后,它显示我的图像!

请帮忙调试。

1 个答案:

答案 0 :(得分:0)

我用这种方式解决了这个问题:

添加:

   function openCamera1(){     
        result = camera.startCameraActivity1();  
        path1=camera.getImagePath1();  
        path="file://"+path1;
        do{  
                 fileIndicator=camera.findEOF();  
             }while(!fileIndicator)  
                document.getElementById("image1").src=path;  
        }

在OpenCamera中添加一个方法:

public boolean findEOF(){  
       File file=new File(imagePath1);  
       System.out.println("Inisde EOFL::::::::::::::"+file.length());  

        if(file.length()>0){ 
           System.out.println("Inisde length is::::::::::::::"+file.length());  
            return true;
      }         
       return false;

   }  

所以问题是在将图像写入文件之前调用了imagePath()。