gpt4 book ai didi

Android:相机在 ActivityResult 上停止

转载 作者:行者123 更新时间:2023-11-29 01:48:20 25 4
gpt4 key购买 nike

我正在尝试构建一个相机应用。

这是 MainActivity 的代码:

public class MainActivity extends Activity {

public static int CAMERA_REQUEST_KEY = 100;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_REQUEST_KEY);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST_KEY) {
if (resultCode == RESULT_OK) {
Bundle bundle = data.getExtras();
Bitmap bmp = (Bitmap) bundle.get("data");
ImageView iv = (ImageView) findViewById(R.id.image_frame);
iv.setImageBitmap(bmp);
} else {
Toast.makeText(getApplicationContext(), "Operation Failed", Toast.LENGTH_LONG).show();
}
}
}
}

拍摄图像后,我收到此消息:
“很遗憾,相机教程已停止”;日志猫说

11-13 21:23:00.107: E/AndroidRuntime(17967): java.lang.RuntimeException: Unable to resume activity {com.example.cameratutorial/com.example.cameratutorial.MainActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=Intent { act=inline-data dat=content://media/external/images/media/20787 typ=image/jpeg (has extras) }} to activity {com.example.cameratutorial/com.example.cameratutorial.MainActivity}: java.lang.NullPointerException

问题是什么?

最佳答案

你没有

  setContentView(R.layout.mylayout);

所以

  ImageView iv = (ImageView) findViewById(R.id.image_frame);// fails

findViewById 查找具有当前膨胀布局中提到的 id 的 View 。所以需要先将layout的内容设置到activity中,再初始化views。

ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
iv = (ImageView) findViewById(R.id.image_frame);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_REQUEST_KEY);
}

确保您在 list 文件中拥有所需的权限

http://developer.android.com/reference/android/hardware/Camera.html

http://developer.android.com/guide/topics/media/camera.html

关于Android:相机在 ActivityResult 上停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19946728/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com