簡體   English   中英

如何從Android中的片段啟動相機

[英]How to start camera from fragment in Android

我的CameraFragment.java文件中需要一個攝像頭功能。 我已經有了相機的代碼,並且它可以在一個空的應用程序中工作(當我將其放在MainActivity中時),但是我不知道將代碼放在我的CameraFragment.java中的位置。

我確實是Android Studio的初學者,但是我無法在互聯網上找到答案。 堆棧溢出的新功能。

CameraFragment.java

public class CameraFragment extends Fragment{

public static final String EXTRA_INFO = "default";

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_camera, container, false);
}
}

而且我的CameraFragment文件中需要此代碼:

public class MainActivity extends AppCompatActivity {

private Button btnCapture;
private ImageView imgCapture;
private static final int Image_Capture_Code = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_camera);
    btnCapture =(Button)findViewById(R.id.btnTakePicture);
    imgCapture = (ImageView) findViewById(R.id.capturedImage);
    btnCapture.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent cInt = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cInt,Image_Capture_Code);
        }
    });
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent 
    data) {
    if (requestCode == Image_Capture_Code) {
        if (resultCode == RESULT_OK) {
            Bitmap bp = (Bitmap) data.getExtras().get("data");
            imgCapture.setImageBitmap(bp);
        } else if (resultCode == RESULT_CANCELED) {
            Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
        }
    }
}
}

讓我知道這對您如何工作。 如果您需要更多幫助進行設置,請發表評論。

public class CameraFragment extends Fragment {
public static final String EXTRA_INFO = "default";
private Button btnCapture;
private ImageView imgCapture;
private static final int Image_Capture_Code = 1;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_camera, container, false);
    btnCapture =(Button) view.findViewById(R.id.btnTakePicture);
    imgCapture = (ImageView) view.findViewById(R.id.capturedImage);
    btnCapture.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent cInt = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cInt,Image_Capture_Code);
        }
    });

    return view;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == Image_Capture_Code) {
        if (resultCode == RESULT_OK) {
            Bitmap bp = (Bitmap) data.getExtras().get("data");
            imgCapture.setImageBitmap(bp);
        } else if (resultCode == RESULT_CANCELED) {
            Toast.makeText(getActivity(), "Cancelled", Toast.LENGTH_LONG).show();
        }
    }
}}

在片段中使用與活動相同的方法,但將方法設為公開,請嘗試使用此代碼

public class ChatFragment extends Fragment {
private RecyclerView chatRecylerview;
View view;
ChatUserlistAdapter userlistAdapter;
LinearLayoutManager manager;
ArrayList<HashMap<String, String>> userDetail = new ArrayList<>();
HashMap<String, String> data;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_chat, container, false);

    btnCapture =(Button)view.findViewById(R.id.btnTakePicture);
    imgCapture = (ImageView)view.findViewById(R.id.capturedImage);
    btnCapture.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent cInt = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cInt,Image_Capture_Code);
        }
    });
    return view;
}


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == Image_Capture_Code) {
        if (resultCode == RESULT_OK) {
            Bitmap bp = (Bitmap) data.getExtras().get("data");
            imgCapture.setImageBitmap(bp);
        } else if (resultCode == RESULT_CANCELED) {
            Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
        }
    }
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM