AAlferez
AAlferez

Reputation: 1502

Camera Intent not breaking code execution until pic taken

So I have a button, which makes the camera intent to appear to take a picture. I will be using that picture to display it. The problem is the code after the new Intent continues executing and breaks because I have no image back yet until I take the pic.

public class WebViewActivity extends Activity {

private WebView webView;
private Uri picUri;
private String finalEncodedImage;
private byte[] finaldata;
FileInputStream finall = null;
private Handler handler = new Handler();

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

    webView = (WebView) findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebViewClient(new MyWebViewClient());


    String otherPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString() + "/Camera/IMG_20140213_142815.jpg";

    File imagefile = new File(otherPath);
    FileInputStream fis = null;
    try {
        fis = new FileInputStream(imagefile);
        finall = fis;
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Bitmap bi = BitmapFactory.decodeStream(fis);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bi.compress(Bitmap.CompressFormat.PNG, 100, baos);

    byte[] data = baos.toByteArray();
    finaldata = data;
    String image64 = Base64.encodeToString(data, Base64.DEFAULT);
    finalEncodedImage = image64;    


    webView.loadUrl("http://myhtml.html");

}


private class MyWebViewClient extends WebViewClient {

    public Intent CameraTakesPic ()
    {
        //OPEN CAMERA AND TAKE PICTURE
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(takePictureIntent, 1);
        }

        return takePictureIntent;

    }

    @Override
    public WebResourceResponse shouldInterceptRequest (final WebView view, String url) {
        if (url.contains("img03")) 
        {               
            //%%%
            //OPEN CAMERA AND TAKE PICTURE

            Intent picIntent = new Intent();
            picIntent = CameraTakesPic();
            //WITH THE PICTURE
            Bundle extras = picIntent.getExtras();
            if (extras != null)
            {
                Bitmap imageBitmap = (Bitmap) extras.get("data");
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
                //MAKE IT A BYTE ARRAY
                byte[] data = baos.toByteArray();
                finaldata = data;

                InputStream is = new ByteArrayInputStream(finaldata);
             //%%%
                return new WebResourceResponse("text/html", "UTF-8", is);
            }
            else
            {
                return null;                    
            }                
         }          
        else
        {
            return null;
        }
        // convert String into InputStream
         //return super.shouldInterceptRequest(view, url);
            //return super.shouldInterceptRequest(view, "<img src=\"data:image/jpeg;base64," + finalEncodedImage + "\" /></img>");

    }
}
public class CameraContentDemoActivity extends WebViewActivity {
      private static final int CONTENT_REQUEST=1337;
      private File output=null;

      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File dir=
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);

        output=new File(dir, "CameraContentDemo.jpeg");
        i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output));

        startActivityForResult(i, CONTENT_REQUEST);
      }

      @Override
      protected void onActivityResult(int requestCode, int resultCode,
                                      Intent data) {
        if (requestCode == CONTENT_REQUEST) {
          if (resultCode == RESULT_OK) {
            Intent i=new Intent(Intent.ACTION_VIEW);

            i.setDataAndType(Uri.fromFile(output), "image/jpeg");
            startActivity(i);
            finish();
          }
        }
      }
    }
}

So I don't want to let the code running until I have my picture taken. What am I doing wrong? I guess opening a new Intent does not mean the rest of the code stops. So which is my solution?

Upvotes: 0

Views: 221

Answers (2)

AAlferez
AAlferez

Reputation: 1502

I ended up creating a boolean and a loop, so my code loops on a sleep while the boolean is false and when I have the picture saved and returned, I set the boolean to true and it exits the loop.

I know is not ideal solution, but works for now.

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006944

I guess opening a new Intent does not mean the rest of the code stops

startActivity() and startActivityForResult() are asynchronous.

So which is my solution?

Process the results of the picture in onActivityResult(). You can see an example of how to do this in the developer documentation and in this sample app:

package com.commonsware.android.camcon;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import java.io.File;

public class CameraContentDemoActivity extends Activity {
  private static final int CONTENT_REQUEST=1337;
  private File output=null;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File dir=
        Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);

    output=new File(dir, "CameraContentDemo.jpeg");
    i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output));

    startActivityForResult(i, CONTENT_REQUEST);
  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode,
                                  Intent data) {
    if (requestCode == CONTENT_REQUEST) {
      if (resultCode == RESULT_OK) {
        Intent i=new Intent(Intent.ACTION_VIEW);

        i.setDataAndType(Uri.fromFile(output), "image/jpeg");
        startActivity(i);
        finish();
      }
    }
  }
}

Upvotes: 3

Related Questions