Droid RAZR M相机通过ACTION_IMAGE_CAPTURE Intent调用时会冻结

时间:2013-10-09 21:49:36

标签: android android-intent camera android-4.0-ice-cream-sandwich

我在使用ICS上的ACTION_IMAGE_CAPTURE Intent调用我的Droid Razr M上的内置相机应用程序时遇到了麻烦,并且希望其他人看到这个并且知道如何解决/解决问题。 我的应用程序启动相机,等待用户捕获并接受图像,然后在返回时处理它。但请注意,下面的示例应用程序中没有处理图像,问题仍然存在。这里的关键要素是我们在从Intent返回时立即重新调用相机,以允许用户一个接一个地继续拍照。这在许多设备(十几种不同的设备)上运行良好,但在运行4.1.2的Droid Razr M(以及早期版本的ICS)上失败了。拍摄第二张照片后显示故障 - 相机上的所有按钮都被禁用,只有后退按钮才能工作。如果我们在重新启动Intent之前在我们的应用程序中延迟5秒或更长时间,则不会发生此问题 - 但这是不可接受的。 这是一个简单的repro应用程序,用于演示此问题(请注意,您需要启用WRITE_EXTERNAL_STORAGE权限才能生效):

public class MainActivity extends Activity {

private static final int RESPONSE_SINGLE_SHOT = 45;
private static final String TAG = "CameraTest";

private String mCameraFilePath = null;

@Override
protected void onCreate( Bundle savedInstanceState ) {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_main );
    onLaunchCamera( true );  //The launch is here only for simplification - it also fails in onStart()
}

@Override
protected void onActivityResult( final int requestCode, final int resultCode, final Intent intent )
{
    super.onActivityResult(requestCode, resultCode, intent);
    processImage( requestCode, resultCode, intent );
}

public void onLaunchCamera( boolean fromUserAction )
{
    final String storageState = Environment.getExternalStorageState();
    if (storageState.equals(Environment.MEDIA_MOUNTED))
    {
        final Intent cameraIntent = new Intent( MediaStore.ACTION_IMAGE_CAPTURE );
        cameraIntent.setFlags( Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET );

        final List<ResolveInfo> list = getPackageManager().queryIntentActivities( cameraIntent, 0 );
        //Grab the first camera in the list, this should be the camera app that came with the device:
        final String packageName = list.get(0).activityInfo.packageName;
        final String name = list.get(0).activityInfo.name;

        final File publicDir = Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_DCIM );
        final Time t = new Time();
        t.setToNow();

        File cameraFile = new File( publicDir, "/Camera/" + makePhotoFileName( t )  );
        mCameraFilePath = cameraFile.getAbsolutePath();
        Log.i( TAG, "creating camera file: " +  mCameraFilePath );

        try
        {
            if ( cameraFile.exists() == false )
            {
                cameraFile.getParentFile().mkdirs();
                if ( cameraFile.createNewFile() )
                {
                    cameraIntent.putExtra( MediaStore.EXTRA_OUTPUT, Uri.fromFile( cameraFile ) );
                } else {
                    Log.e( TAG, "failed to create file:" + cameraFile.getAbsolutePath() );
                    return;
                }
            }

        } catch ( IOException e )
        {
            Log.e( TAG, "Could not create file.", e );
            return;
        }

        cameraIntent.setComponent( new ComponentName( packageName, name ) );
        startActivityForResult( cameraIntent, RESPONSE_SINGLE_SHOT );

    } else {
        Log.e(TAG, "SD card not present");
    }
}

private void processImage( final int requestCode, final int resultCode, final Intent intent ) {
    switch (requestCode)
    {
        case RESPONSE_SINGLE_SHOT:
            if ( resultCode == RESULT_OK )
            {       
                runOnUiThread( new Runnable() {
                    @Override
                    public void run() {
                        onLaunchCamera( false );    //Immediately re-run camera
                    }
                });
            }
            else {
                // delete the placeholder file we created
                if ( mCameraFilePath != null ) {
                    final File cameraFile = new File( mCameraFilePath );
                    cameraFile.delete();
                    mCameraFilePath = null;
                }
            }
            break;
        default:
            break;
    }
}

private String makePhotoFileName( final Time t ) {
    final String fileName = t.format("IMG_%Y%m%d_%H%M%S") + ".jpg";
    return fileName;
}
}

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

我们能够为Razr M上的默认相机提供的唯一解决方案是在拍摄图像后不立即返回相机。我们通过在onLaunchCamera()中的try-catch块之前插入以下代码来完成此操作:

if ( !fromUserAction && name.equals( "com.motorola.camera.Camera" ) && Build.MODEL.equals( "XT907" ) )
    return;

这只是一种解决方法,但确实可以防止相机挂起。