使用手机相机拍照后,活动不返回到特定的活动。

5
我正在使用手机相机并使用android.provider.MediaStore.ACTION_IMAGE_CAPTURE从我的应用程序中捕获图像。
点击拍摄按钮后,它会显示保存或丢弃。点击保存按钮后,它返回到相机而不是应用程序。图像保存在画廊中而不是我声明的输出文件中。
以下是我的代码:
    @Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    Intent in=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(in, 0);

}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode==RESULT_OK) {
    Bitmap bm=(Bitmap) data.getExtras().get("data");
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.JPEG, 40, bytes);

    //you can create a new file name "test.jpg" in sdcard folder.
    File f = new File(Environment.getExternalStorageDirectory()
                            + File.separator + "player1.jpg");
    try {
        f.createNewFile();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    //write the bytes in file
    FileOutputStream fo = null;
    try {
        fo = new FileOutputStream(f);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        fo.write(bytes.toByteArray());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }finally{
        Intent pic=new Intent(pic1.this, GameMenu.class);
        startActivity(pic);
        finish();
    }
}
else {
    Toast.makeText(getApplicationContext(), "Canceled", Toast.LENGTH_LONG).show();
    Intent pic=new Intent(pic1.this, Menu.class);
    startActivity(pic);
    finish();
}

请提供任何解决方案建议。 提前感谢。

编辑1:我已在三星Galaxy Y Duos(Android 2.3.6)上检查了此代码,它不能正常工作。在Galaxy Pop(2.2.1)和HTC Wildfire(2.3.5)上检查了相同的代码,它可以完美运行。


你尝试过在 onActivity Result 里检查响应代码吗? - Parth Kapoor
5个回答

2

使用以下意图来捕获图像,getTempFile() 是您在其中提及存储路径的位置。

Intent photoPickerIntent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                       photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, getTempFile());

                       photoPickerIntent.putExtra("outputFormat",Bitmap.CompressFormat.JPEG.toString());
                       photoPickerIntent.putExtra("return-data", true);
                       startActivityForResult(photoPickerIntent,TAKE_PICTURE);

getTempFile:

private Uri getTempFile() {


        File root = new File(Environment.getExternalStorageDirectory(), "Directory");
        if (!root.exists()) {
            root.mkdirs();
        }
             File file;

             file = new File(root,filename+".jpeg" );

             muri = Uri.fromFile(file);
             photopath=muri.getPath();

              Log.e("getpath",muri.getPath());
              return muri;

           }

请点击这里查看更多有关如何使用本机安卓相机捕获图像并存储的讨论线程。

谢谢伙计,但是对于Galaxy Duos而言结果仍然是一样的,其他方面都正常。 - Nipam Shah
Galaxy Duos 发生了什么? - Abhi

0
如果这是你的整个代码,那么你已经省略了setContentView(); 这可能是它没有返回的原因。
  @Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
     // setContentView() here
    Intent in=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(in, 0);

}

onActivityResult()方法被调用了吗?


0

你应该打开相机来创建文件。将文件URI作为额外信息传递给相机意图:

/**
 * Capturing Camera Image will lunch camera app request image capture
 */
private void captureImage() {

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    fileUri = getOutputMediaFileUri();

    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

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

这是我创建文件的方式:

/**
 * Creating file uri to store image
 * 
 * 
 * @return Uri
 */
public Uri getOutputMediaFileUri() {
    return Uri.fromFile(getTempOutputMediaFile());
}

/**
 * returning File
 * 
 * 
 * @return File
 */
private File getTempOutputMediaFile() {

    File appDirectory = this.getFilesDir();

    File mediaStorageDir = new File(APP_NAME,
                YOUR_IMAGE_DIRECTORY_NAME);

    Log.i("mediaStorageDir", mediaStorageDir.getAbsolutePath());

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.i(YOUR_IMAGE_DIRECTORY_NAME,
                    "Oops! Failed create "
                            + YOUR_IMAGE_DIRECTORY_NAME
                            + " directory");
            return null;
        }
    }

    // Create a media file name
    UUID temporaryImageUuid = UUID.randomUUID();
    File mediaFile;

    mediaFile = new File(mediaStorageDir.getPath() + File.separator
            + temporaryImageUuid + "." + YOUR_EXTENSION);

    return mediaFile;
}

我需要直接将我的图像文件放在内部存储路径 - getFilesDir / Appname 中,而不是外部存储。 - joao2fast4u
我更新了我的回答,图片保存在本地存储中。 - ahmed_khan_89
主要问题是并不总是能在onActivityResult中得到三星设备的结果...为什么会这样?我只是想找出原因。但还是谢谢你的努力。 - joao2fast4u
你可能需要更新你的问题为“为什么三星设备有时候在使用startActivityForResult()时表现出奇怪的行为?为什么?”祝好运 :) - ahmed_khan_89

0

嗨,请检查以下代码。在捕获图像后使用startPreview()释放。这对我有用。

        captureButton.setOnClickListener(new View.OnClickListener() {
                        @Override
            public void onClick(View v) {
                            mCamera.autoFocus(null);
                mCamera.takePicture(null, null, mPicture);
                Log.e("in capture button","onclick of this button"+ mCamera);
                mCamera.startPreview();
                                }
        });

你什么时候使用这段代码?captureButton在哪里? - joao2fast4u
这里并不是直接使用Intent调用相机。首先需要创建一个按钮,然后实现这段代码。这类似于自定义相机,我们可以添加所需的功能。 - image

0

我遇到了同样的问题,我重启了设备并测试了我的应用程序,现在它可以正常工作了。可能是由于不同的代码实现导致应用程序无响应。尝试重新启动设备并进行测试,然后告诉我是否有效。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接