简体   繁体   中英

Strange behavior with Camera

I am developing a hybrid app using android,js and html5.

My code is as follows:

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);

  }

similarly camer2 method. And js and 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);   

... }

Here "Data" is null!! How is it possible? Is it right what am i doing here??

The problem is that if i comment the alert(path1) and alert(path2) i am not able to load the image on img tag. Its displaying "?" instead. After i enable the alert or try for 3-4 times capturing the image, it is displaying me the image!

Please help to debug.

I resolved it in this way :

add :

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

And in OpenCamera add a method:

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;

   }  

So the problem was get imagePath() was being called before the image is written to the file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM