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

标签 java android

我是 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/

相关文章:

javascript - 如何在action类中识别两个不同的jsp页面按钮?如何编写条件,基于jsp页面查看按钮和查看全部按钮?

java - 可以在运行时在 ViewPager 中添加/删除选项卡吗?

java - 如何删除重复的 RecyclerView 适配器?

android - res/drawable/Icon.png 位于 Titanium Studio IDE 中的什么位置?

Android Groovy 网络服务

android - 安装 apk "Open"和 "Done"按钮显示后,单击打开按钮不会将类别添加为启动器

java - 在 mybatis 中缓存查询 - 可能的方法

java - 如何将主题的所有记录存储在单个文件中?

java - 将字符串中的最后一个点替换为美元符号

android - OnUpdate() 不调用小部件服务