简体   繁体   中英

Android send Camera Preview from Activity to Fragment TextureView

There is a TextureView in my Fragment and Fragment must be re-inflate a Layout. I using Camera API in Activity (PS: Camera API must be call in Activity), and I can use SurfaceTexture to get Camera Preview.

Question how to make TextureView can get Camera preview. I tired many solutions but all of them got null object reference.

public class MyFragment extends PresentationFragment{
    private TextureView textureView;
    private TextView textView;
    public View onCreateView(LayoutInflater layoutInflater, @Nullable ViewGroup contatiner, Bundle savedInstanceState) {
        rootView = layoutInflater.inflate(R.layout.external_display, contatiner, false);
        textureView = rootView.findViewById(R.id.camera_view);
        textView = rootView.findViewById(R.id.test_text);

        return rootView;
    }

}

In Activity:

private MyFragment myFragment;
private TextureView textureView;
private SurfaceTexture surfaceTexture;

In onCreate:

myFragment =(myFragment)getFragmentManager().findFragmentById(R.id.external_display);
View view = myFragment.getView();
textureView = view.findViewById(R.id.camera_preview);
surfaceTexture = textureView.getSurfaceTexture();

getView() null object reference.

That's the Activity lifecycle for you. The fragment views are not available in onCreate. Luckily, this should not be a problem: it's not correct to initialise the camera before onRestore.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM