Reputation: 6134
I have a hybrid application, which opens native camera of android. My code :
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: function captureImage1(){ camera.startCamera(); var path="file://" + camera.getPath(); //alert(path); document.getElementById("image1").src=path; }
public OpenCamera (WebView appView,Activity context) {
this.mAppView = appView;
this.context=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);
Sometimes it dispalays the image at img tag src, sometimes it doesn't. Need help to find out the problem.
Also, is it something related to Orientation change??
Thanks
Upvotes: 0
Views: 169
Reputation: 6134
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
Upvotes: 0