Theory
एक Photo, बिना Camera Build किए
FestConnect Mobile चाहता है users एक profile photo add करें, या एक event पर एक picture snap करें। इसके लिए CAMERA चाहिए।
Exactly sharing की तरह (implicit-intent lesson), आप अपने app में एक camera BUILD नहीं करते। इसकी बजाय, आप device के existing CAMERA app से photo लेने और इसे वापस देने के लिए ASK करते हैं। यह वही cooperate-with-other-apps philosophy है: capable apps को reinvent करने की बजाय reuse कीजिए। यह final technical lesson MediaStore.ACTION_IMAGE_CAPTURE से एक image capture करना cover करता है, camera launch करना और result receive करना, FestConnect Mobile की device features complete करते हुए।
Theory
ACTION_IMAGE_CAPTURE: Camera App Reuse कीजिए
एक photo लेने के लिए, action MediaStore.ACTION_IMAGE_CAPTURE वाला एक implicit intent fire कीजिए। यह device का camera app launch करता है; user picture लेता है, और RESULT आपकी activity को वापस आता है।
चूँकि आप कुछ BACK expect करते हैं (photo), आप इसे A RESULT EXPECT करते हुए launch करते हैं:
- historically
onActivityResult(...)के साथstartActivityForResult(...) - modern Android Activity Result APIs (
registerForActivityResult) इस्तेमाल करता है, cleaner और recommended
Captured image या तो result के extras में (key "data" के under) एक छोटे THUMBNAIL bitmap की तरह वापस आती है, या, एक FULL-SIZE image के लिए, आप camera को बताते हैं इसे WHERE save करना है EXTRA_OUTPUT से (एक file URI, modern Android पर एक FileProvider के through provided)। यह फिर वही implicit-intent pattern है: आपका app एक capability trigger करता है, camera app इसे perform करता है, result वापस आता है।
Practical
Camera Launch कीजिए और Photo Receive कीजिए
// Modern approach: register a launcher for the camera result
val cameraLauncher = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) { result ->
if (result.resultCode == Activity.RESULT_OK) {
// A thumbnail comes back in the extras under "data":
val photo = result.data?.extras?.get("data") as? Bitmap
// ... show or save the photo
}
}
fun takePhoto() {
// Implicit intent: ask the device's camera app to capture an image
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
cameraLauncher.launch(intent) // launch, result returns to the callback
}
// For a full-size image, add EXTRA_OUTPUT with a FileProvider URI.
Quiz
FestConnect Mobile के लिए एक user को photo लेने देने का सबसे simple तरीका क्या है?
- App के अंदर scratch से एक पूरा camera interface build कीजिए
- Device के camera app को launch करने के लिए MediaStore.ACTION_IMAGE_CAPTURE वाला एक implicit intent fire कीजिए, और photo को एक result की तरह वापस receive कीजिए
- Camera access करना impossible है
- Internet से एक photo copy कीजिए
Show the answer
Device के camera app को launch करने के लिए MediaStore.ACTION_IMAGE_CAPTURE वाला एक implicit intent fire कीजिए, और photo को एक result की तरह वापस receive कीजिए
Simplest, standard approach MediaStore.ACTION_IMAGE_CAPTURE वाला एक IMPLICIT intent fire करना है, जो device के existing camera app को launch करता है; user photo लेता है और result आपकी activity को वापस आता है (Activity Result APIs या older onActivityResult के through)। यह capable camera app को reinvent करने की बजाय reuse करता है, sharing वाला same cooperate-with-other-apps pattern। Option A (scratch से camera build करना) enormous, error-prone work है जो वही duplicate करता है जो device already अच्छी तरह करता है, सिर्फ़ advanced custom-camera cases के लिए चाहिए। Option C false है। Option D पूरा point miss करता है (capturing, downloading नहीं)। ACTION_IMAGE_CAPTURE से camera app reuse कीजिए और returned image handle कीजिए।
Think first
Camera AND Sharing के पीछे का Pattern
Sharing (earlier lesson) और एक photo लेना दोनों एक task को दूसरे app को हाथ में देने के लिए implicit intents इस्तेमाल करते हैं। यह कौन सा general Android design principle reveal करता है? फिर tap कीजिए।
Show the answer
Principle features को reinvent करने की बजाय IMPLICIT INTENTS के through CAPABLE APPS REUSE करना है। Android इस तरह designed है कि apps CAPABILITIES expose करते हैं (एक camera app images capture कर सकता है; एक browser URLs view कर सकता है; एक messaging app text send कर सकता है), और दूसरे apps implicit intents से उन capabilities को INVOKE करते हैं, system को इन्हें connect करने देते हुए। तो हर app को अपना खुद camera, browser, या share sheet build करने की बजाय, apps COOPERATE करते हैं: FestConnect ACTION_IMAGE_CAPTURE fire करता है और user का chosen camera app काम करता है, फिर result return करता है। Benefits: कहीं कम code (आप एक camera build नहीं करते), एक consistent experience (user का familiar camera app), user के पास जो भी apps हों उनके लिए automatic support, और separation of concerns। यह अच्छे Android development का एक hallmark है: आपका app अपने JOB पर focus करता है (events manage करना) और specialised tasks (photos, sharing, viewing) उनके लिए बने apps को delegate करता है। Device की capabilities compose कीजिए; इन्हें duplicate मत कीजिए।
Watch out
Camera-Capture Traps
Unnecessarily एक Camera Build करना: एक simple photo के लिए, ACTION_IMAGE_CAPTURE और camera app इस्तेमाल कीजिए।
Result Code Ignore करना: photo इस्तेमाल करने से पहले RESULT_OK check कीजिए (user cancel कर सकता है)।
Thumbnail बनाम Full Image: extras "data" एक छोटा thumbnail देता है; full-size के लिए, EXTRA_OUTPUT + एक FileProvider URI इस्तेमाल कीजिए।
Modern Android पर FileProvider: एक raw file URI pass करना newer Android पर crash कर सकता है; एक FileProvider इस्तेमाल कीजिए।
कोई Camera App नहीं: rare है, पर robust apps उस case को handle करते हैं जहाँ कोई camera app exist नहीं करता।
Theory
Unit 3 Complete: एक Full Mobile App
Kotlin में FestConnect Mobile अब feature-complete है: यह एक server से JSON parse करता है, intents से multiple screens navigate करता है, share करता है और URLs खोलता है, SQLite में data store करता है, और location और camera इस्तेमाल करता है, एक real, data-driven, device-aware Android app। यही mobile-development arc है। Unit 4 अलग है: Lilavati से Indian Knowledge System mathematics, जिसे factually पढ़ा जाता है, वही mathematics unit BCA503-01 के साथ shared। App built है; IKS unit अलग खड़ी है।
Summary
Key takeaways
- एक photo लेने के लिए, MediaStore.ACTION_IMAGE_CAPTURE वाला एक implicit intent fire कीजिए, device के camera app को launch करते हुए।
- आप इसे A RESULT expect करते हुए launch करते हैं: modern Android Activity Result APIs (registerForActivityResult) इस्तेमाल करता है; older code startActivityForResult/onActivityResult इस्तेमाल करता था।
- एक thumbnail bitmap "data" के under result extras में return होता है; full-size image के लिए, EXTRA_OUTPUT provide कीजिए (FileProvider के through एक file URI)।
- Photo इस्तेमाल करने से पहले RESULT_OK check कीजिए (user cancel कर सकता है)।
- यह एक camera build करने की बजाय camera app reuse करता है: sharing वाला same implicit-intent, cooperate-with-other-apps pattern।
- General principle: दूसरे apps की capabilities implicit intents से invoke कीजिए, इन्हें duplicate करने की बजाय।
- Memory hook: ACTION_IMAGE_CAPTURE camera app से पूछता है; photo एक result की तरह वापस आता है।