Android 2.2 SDK - Droid X Camera Activity无法正常完成

时间:2011-02-04 18:45:06

标签: java android mobile android-2.2-froyo

我注意到我在Droid X上调用的默认摄像头活动与我的Droid和Nexus One上的默认摄像头活动不同。在Droid和Nexus One上选择“OK”后,活动就会完成 - Droid X有一个“完成”按钮(它会带你回到相机,而不是完成活动),这是进入我想要的屏幕是点击“后退”按钮。

以下是适用于Android 2.2 / 2.3的课程,但不适用于Droid X的课程:

package com.android.xxx;

import java.io.File;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Window;

public class CameraView extends MenusHolder {

    protected String _path;
    protected boolean _taken;

    protected static final String PHOTO_TAKEN = "photo_taken";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.create_event_view);
        /*
         * save to sd
         */
        File imageDirectory = new File(
                Environment.getExternalStorageDirectory() + "/MyPath/");
        imageDirectory.mkdirs();
        /*
         * temp image overwrites each time for space
         */
        _path = Environment.getExternalStorageDirectory()
                + "/MyPath/temporary_image.jpg";
        startCameraActivity();
    }

    protected void startCameraActivity() {
        File file = new File(_path);
        Uri outputFileUri = Uri.fromFile(file);

        Intent intent = new Intent(
                android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        startActivityForResult(intent, 0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (resultCode) {
        case 0:
            setResult(5);
            finish();
            break;
        case -1:
            onPhotoTaken();
            break;
        }
    }

    protected void onPhotoTaken() {
        _taken = true;
        setResult(0);
        finish();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putBoolean(CameraView.PHOTO_TAKEN, _taken);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        if (savedInstanceState.getBoolean(CameraView.PHOTO_TAKEN)) {
            onPhotoTaken();
        }
    }

}

2 个答案:

答案 0 :(得分:1)

我用一个非常丑陋的解决方法解决了这个问题。我编写了两个函数来读取和写入sdcard中的文件(取自这里:http://www.sgoliver.net/blog/?p=2035)。

private boolean readFile() {
    try
    {
        File sd_path = Environment.getExternalStorageDirectory();

        File f = new File(sd_path.getAbsolutePath(), "lock_camera_oncreate");

        BufferedReader fin =
            new BufferedReader(
                new InputStreamReader(
                    new FileInputStream(f)));

        String text = fin.readLine();
        fin.close();
        Log.e("Files", "Reading file");
        return true;
    }
    catch (Exception ex)
    {
        Log.e("Files", "Error reading file from SD Card");
        return false;
    }
}

private void createFile() {
    try
    {
        File sd_path = Environment.getExternalStorageDirectory();

        File f = new File(sd_path.getAbsolutePath(), "lock_camera_oncreate");

        OutputStreamWriter fout =
            new OutputStreamWriter(
                new FileOutputStream(f));

        fout.write("Semaphore test.");
        fout.close();
        Log.e("Files", "File writed");
    }
    catch (Exception ex)
    {
        Log.e("Files", "Error reading file from SD Card");
    }

}

然后,在onCreate函数中,我做了这个:

public void onCreate(Bundle savedInstanceState) {
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    super.onCreate(savedInstanceState);

    if(readFile() == true)
    {
        File sd_path = Environment.getExternalStorageDirectory();
        File f = new File(sd_path.getAbsolutePath(), "lock_camera_oncreate");
        f.delete();

        Intent intent = this.getIntent();
        this.setResult(RESULT_OK, intent);
        return;
    }

    createFile();

    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(mCurrentImagePath)));
    startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
}

当您在纵向模式下使用应用程序时,setRequestedOrientation调用解决了问题,但是当启动相机时,您将移动设备置于横向状态,然后拍摄照片。

然后,丑陋的readFile事件检查是否存在lock_camera_oncreate文件,如果它是真的, 然后发生了额外的onCreate调用,所以从此活动中删除文件和RETURN。

如果活动进展,则表示文件未创建且只有一个摄像机活动正在运行。

希望它有所帮助,它很丑,但对我有用:D

答案 1 :(得分:0)

老兄......这只是一个错误。我有同样的问题,没有办法解决这个问题。它有时会起作用,有时却不起作用。我向摩托罗拉的家伙寻求帮助,他们表示不支持这些Android图片。