gpt4 book ai didi

java - 构建相机应用程序 - 接收

转载 作者:行者123 更新时间:2023-12-01 05:33:05 31 4
gpt4 key购买 nike

我是 Android 编程新手,我正在用 Java 编写一个应用程序,该应用程序可以打开相机拍照并保存。我通过 Intents 做到了,但看不到 onActivityResult 正在运行。

我已经在我的手机(三星 Galaxy S)中对其进行了测试,当我拍照时,我会收到该照片的预览,其中有两个按钮,一个是“保存”,另一个是“取消”。我还没有在我的代码中添加一些东西来执行此操作,所以我认为这是相机所做的事情。我希望在捕获图像后运行 onActivityResult (在按下预览上的“保存”按钮后)。

但是在预览中按下“保存”按钮后,如何返回结果以启动 onActivityResult

我忘记告诉大家,在我按下“保存”后,我的整个应用程序将被终止。这是我的代码

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

TakePicButton = (Button) findViewById(R.id.TakePicture);
TakePicButton.setOnClickListener((android.view.View.OnClickListener) this);

}

@Override
public void onDestroy(){
super.onDestroy();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
Toast.makeText(this, "Image saved to:\n" + data.getData(), Toast.LENGTH_LONG).show();

} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT);

} else {
Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT);

}
}

public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId() == R.id.TakePicture){

// create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
}

最佳答案

尝试下面的代码,你需要稍微修改一下,它将帮助你从库和相机中获取,SELECT_PICTURE用于从库中获取图像

public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case SELECT_PICTURE:
Uri selectedImageUri = data.getData();
filemanagerstring = selectedImageUri.getPath();
selectedImagePath = getPath(selectedImageUri);
if (selectedImagePath != null)
myFile = new File(selectedImagePath);
else if (filemanagerstring != null)
myFile = new File(filemanagerstring);
if (myFile != null) {
Bitmap bmp_fromGallery = decodeImageFile(selectedImagePath);


break;
case CAMERA_REQUEST:

Bitmap bmp_Camera = (Bitmap) data.getExtras().get("data");

break;
default:
break;
}
}

关于java - 构建相机应用程序 - 接收,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8645290/

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