Android 2.2 SDK - Droid X Camera Activity doesn't finish properly

删除回忆录丶 提交于 2019-12-01 11:53:25

Dude... it's just a bug. I had the same problem and there's no way to workaround that. It sometimes work, and sometimes it does not. I asked the Motorola's guy for help and they said that there's no support for those Android images.

I solved this with a really really ugly workaround. I coded two functions to read and write files from sdcard (taken from here: 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");
    }

}

Then, in onCreate function, I make this:

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);
}

The setRequestedOrientation call solves the issue when you are using your app in portrait mode, but when camera is launched, you put the mobile in landscape and then shoot the photo.

Then, the ugly readFile thing checks if a lock_camera_oncreate file exists and if it's true, then an additional onCreate call happened, so delete file and RETURN from this activity.

If activity advances, means the file's not created and there is only one camera activity running.

Hope it helps, it's ugly but worked for me :D

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!