Android使用Intent调用摄像头并获取照片

倖福魔咒の 提交于 2019-11-30 11:36:44

使用Android的Intent调用另外一个activity的时候,采用的是多线程机制,异步方式。startActivityForResult之后被调用activity并没有马上返回结果给调用activity,Android的Acitivity对象中startActivityForResult的源代码中有相关的解释。

    

/**

     * Launch an activity for which you would like a result when it finished.

     * When this activity exits, your

     * onActivityResult() method will be called with the given requestCode.

     * Using a negative requestCode is the same as calling

     * {@link  #startActivity} (the activity is not launched as a sub-activity).

     *

     * <p>Note that this method should only be used with Intent protocols

     * that are defined to return a result.  In other protocols (such as

     * {@link  Intent#ACTION_MAIN} or {@link  Intent#ACTION_VIEW}), you may

     * not get the result when you expect.  For example, if the activity you

     * are launching uses the singleTask launch mode, it will not run in your

     * task and thus you will immediately receive a cancel result.

     *

     * <p>As a special case, if you call startActivityForResult() with a requestCode

     * >= 0 during the initial onCreate(Bundle savedInstanceState)/onResume() of your

     * activity, then your window will not be displayed until a result is

     * returned back from the started activity.  This is to avoid visible

     * flickering when redirecting to another activity.

     *

     * <p>This method throws {@link  android.content.ActivityNotFoundException}

     * if there was no Activity found to run the given Intent.

     *

     * @param  intent The intent to start.

     * @param requestCode If >= 0, this code will be returned in

     *                    onActivityResult() when the activity exits.

     * @param options Additional options for how the Activity should be started.

     * See {@link android.content.Context#startActivity(Intent, Bundle)

     * Context.startActivity(Intent, Bundle)} for more details.

     *

     * @throws android.content.ActivityNotFoundException

     *

     * @see #startActivity

     */

public void startActivityForResult(Intent intent, int requestCode, @Nullable Bundle options)


》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》

Button pButton = (Button) findViewById(R.id.btn_return);

pButton.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

// TODO Auto-generated method stub

Intent pIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//调用摄像头action

startActivityForResult(pIntent,INTENT_CODE_IMAGE_CAPTURE);//requestcode

                            //startActivityForResult如果就马上获取intent对象的结果中很多成员是null

}

});



protected void onActivityResult(int requestCode, int resultCode, Intent data) {

// TODO Auto-generated method stub

super.onActivityResult(requestCode, resultCode, data);

if (requestCode==INTENT_CODE_IMAGE_CAPTURE && data != null) {

final ImageView pImageView =(ImageView)findViewById(R.id.imageview1);

Bundle pBundle = data.getExtras(); //从intent对象中获取数据,

if (pBundle != null) {

Bitmap pBitmap = (Bitmap) pBundle.get("data");

if (pBitmap !=null) {

pImageView.setImageBitmap(pBitmap);

pImageView.refreshDrawableState();

Log.i("Result", "capture picture succeed");

}

else {

Log.i("Result", "capture picture failure");

}

}

}

else if (requestCode == 0) {

Toast.makeText(this, "te", Toast.LENGTH_LONG).show();

Log.i("other", "result");

}

}

关于Android的intent机制可参考:

http://www.oschina.net/question/565065_67909

《Android开发精要》有非常全面详细的介绍


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!