Android 拍照前不中断代码执行

Android 拍照前不中断代码执行,android,android-intent,camera,Android,Android Intent,Camera,所以我有一个按钮,它使相机看起来像是在拍照。 我将用那张图片来显示它。 问题是在新意图继续执行并中断后的代码,因为在我拍摄照片之前,我还没有返回图像 public class WebViewActivity extends Activity { private WebView webView; private Uri picUri; private String finalEncodedImage; private byte[] finaldata; FileInputStream final

所以我有一个按钮,它使相机看起来像是在拍照。 我将用那张图片来显示它。 问题是在新意图继续执行并中断后的代码,因为在我拍摄照片之前,我还没有返回图像

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();
          }
        }
      }
    }
}
公共类WebViewActivity扩展活动{
私有网络视图;
私有Uri-picUri;
私有字符串finalEncodedImage;
专用字节[]最终数据;
FileInputStream finall=null;
私有处理程序=新处理程序();
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView=(webView)findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
setWebViewClient(新的MyWebViewClient());
字符串otherPath=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY\u DCIM.toString()+“/Camera/IMG\u 20140213\u 142815.jpg”;
File imagefile=新文件(其他路径);
FileInputStream fis=null;
试一试{
fis=新文件输入流(imagefile);
finall=fis;
}catch(filenotfounde异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
位图bi=BitmapFactory.decodeStream(fis);
ByteArrayOutputStream bas=新的ByteArrayOutputStream();
压缩(Bitmap.CompressFormat.PNG,100,baos);
字节[]数据=baos.toByteArray();
最终数据=数据;
字符串image64=Base64.encodeToString(数据,Base64.DEFAULT);
finalEncodedImage=image64;
webView.loadUrl(“http://myhtml.html");
}
私有类MyWebViewClient扩展了WebViewClient{
公共目的摄像系统()
{
//打开相机拍照
Intent takePictureIntent=新的意图(MediaStore.ACTION\u IMAGE\u CAPTURE);
if(takePictureContent.resolveActivity(getPackageManager())!=null){
startActivityForResult(图片内容1);
}
返回图片内容;
}
@凌驾
公共WebResourceResponse应InterceptRequest(最终WebView视图,字符串url){
if(url.contains(“img03”))
{               
//%%%
//打开相机拍照
Intent picIntent=新Intent();
picIntent=CameraTakesPic();
//带着照片
Bundle extras=picIntent.getExtras();
如果(附加值!=null)
{
位图图像位图=(位图)附加.get(“数据”);
ByteArrayOutputStream bas=新的ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.PNG,100,baos);
//让它成为一个字节数组
字节[]数据=baos.toByteArray();
最终数据=数据;
InputStream is=新的ByteArrayInputStream(finaldata);
//%%%
返回新的WebResourceResponse(“文本/html”,“UTF-8”,is);
}
其他的
{
返回null;
}                
}          
其他的
{
返回null;
}
//将字符串转换为InputStream
//返回super.shouldInterceptRequest(视图,url);
//返回super.shouldInterceptRequest(视图“”);
}
}
公共类CameraContentDemoActivity扩展WebViewActivity{
私有静态最终int-CONTENT_请求=1337;
私有文件输出=null;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
意向i=新意向(MediaStore.ACTION\u IMAGE\u CAPTURE);
文件目录=
getExternalStoragePublicDirectory(Environment.DIRECTORY\u DCIM);
输出=新文件(dir,“CameraContentDemo.jpeg”);
i、 putExtra(MediaStore.EXTRA_输出,Uri.fromFile(输出));
startActivityForResult(一、内容请求);
}
@凌驾
ActivityResult上受保护的void(int请求代码、int结果代码、,
意图(数据){
if(requestCode==CONTENT\u请求){
if(resultCode==RESULT\u OK){
意向i=新意向(意向.行动\视图);
i、 setDataAndType(Uri.fromFile(输出),“image/jpeg”);
星触觉(i);
完成();
}
}
}
}
}
所以我不想让代码在我拍照之前运行。 我做错了什么? 我猜打开一个新的意图并不意味着代码的其余部分停止。 那么,我的解决方案是什么

我猜打开一个新的意图并不意味着代码的其余部分停止

startActivity()
startActivityForResult()
是异步的

那么,我的解决方案是什么

在ActivityResult()上处理图片的结果。您可以在和中看到如何执行此操作的示例:

我猜打开一个新的意图并不意味着代码的其余部分停止

startActivity()
startActivityForResult()
是异步的

那么,我的解决方案是什么

在ActivityResult()上处理图片的结果。您可以在和中看到如何执行此操作的示例:

我猜打开一个新的意图并不意味着代码的其余部分停止

startActivity()
startActivityForResult()
是异步的

那么,我的解决方案是什么

在ActivityResult()上处理图片的结果。您可以在和中看到如何执行此操作的示例:

我猜打开一个新的意图并不意味着代码的其余部分停止

startActivity()
startActivityForResult()
是异步的

那么,我的解决方案是什么

在ActivityResult()上处理图片的结果。您可以在和中看到如何执行此操作的示例:


我最终创造了一个强大的团队
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();
      }
    }
  }
}