0

我有这些代码。打开相机,拍照,但是当我点击“确定”按钮时,什么也没发生。回到以前的活动的唯一方法是单击对我没有用的“x”按钮:)。问题是什么?(onActivityResult 方法尚未完成。)(我曾经将此算法与Android.provider.MediaStore.ACTION_IMAGE_CAPTURE意图一起使用。一切正常。我不知道为什么我现在有问题。)

public void onClick(View v) {

            Intent getCameraImage = new Intent("android.media.action.IMAGE_CAPTURE");
            db.open();
            Cursor cr = db.getAllRecords();
            int count = cr.getCount();

            db.close();

            File cameraFolder;

            if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
                cameraFolder = new File(android.os.Environment.getExternalStorageDirectory(),"TSFC");
            else
                cameraFolder= ShowMessagesPage.this.getCacheDir();
            if(!cameraFolder.exists())
                cameraFolder.mkdirs();

            File photo = new File(Environment.getExternalStorageDirectory(), "TSFC/" + (count + 1) + ".jpg");
            getCameraImage.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));

            initialURI = Uri.fromFile(photo);

            startActivityForResult(getCameraImage, CAMERA_RESULT);
        }});}

protected void onActivityResult(int requestCode, int resultCode,
          Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);

        if (resultCode == RESULT_OK) {
            Bundle extras = intent.getExtras();
            photo = (Uri) extras.get("data");


        }
      }
4

2 回答 2

1

好的,它众所周知的错误,

只需添加此代码行,photo.createNewFile();

就像是,

File photo = new File(Environment.getExternalStorageDirectory(), "TSFC/" + (count + 1) + ".jpg");
photo.createNewFile();
getCameraImage.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));

也别忘了,

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

让我知道你的进展。

于 2013-01-12T17:58:36.423 回答
0

使用onActivityResult(..)方法以这种方式返回结果。

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

    if (requestCode == CAMERA_RESULT) {
        // Make sure the request was successful

            if (resultCode == RESULT_OK) {
            Bundle extras = intent.getExtras();
            photo = (Uri) extras.get("data");

        }
    }
}
于 2013-01-12T17:34:31.733 回答