自动秒收录

从零开始创建一个PhotoApp使用CameraX和Compose:在JetpackCompose中从图库选择照片


文章编号:2869 / 分类:技术教程 / 更新时间:2024-05-23 10:24:55 / 浏览:

专注SEO优化分享SEO技术

//从零开始创建一个PhotoApp使用CameraX和Compose:在JetpackCompose中从图库选择照片

valemptyImageUri=Uri.parse("file://dev/null")Image(modifier=Modifier.fillMaxSize(),painter=rememberAsyncImagePainter(imageUri),contentDescription="Capturedimage")

所以如果要显示图片,我们只要从图库中选中一张图片,然后转换成URI的形式就可以。

首先我们要向AndroidManifest.xml 从零开始创建一个PhotoApp使用Came文件加入下面的媒体访问权限:

图库图片

之后我们还需要一个函数,来打开图库:

@ComposablefunGallerySelect(modifier:Modifier=Modifier,onImageUri:(Uri)->Unit={}){valcontext=LocalContext.currentvallauncher=rememberLauncherForActivityResult(contract=ActivityResultContracts.GetContent(),onResult={uri:Uri?->onImageUri(uri?:EMPTY_IMAGE_URI)})

这里我们申明一个activity的Launcher,使用SideEffect方式来调用:

SideEffect{launcher.launch("image/*")}

这将会过滤显示所有MIME为image类型的文件,也就是所有图片,在Android10以上,需要申请权限,我们将代码改造下,完整如下:

@ComposablefunGallerySelect(modifier:Modifier=Modifier,onImageUri:(Uri)->Unit={}){valcontext=LocalContext.currentvallauncher=rememberLauncherForActivityResult(contract=ActivityResultContracts.GetContent(),onResult={uri:Uri?->onImageUri(uri?:EMPTY_IMAGE_URI)})@ComposablefunLaunchGallery(){SideEffect{launcher.launch("image/*")}}if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.Q){Permission(permission=Manifest.permission.ACCESS_MEDIA_LOCATION,rationale="Youwanttoreadfromphotogallery,soI'mgoingtohavetoaskforpermission.",permissionnotavailableContent={Column(modifier){Text("Onoes!NoPhotoGallery!")Spacer(modifier=Modifier.height(8.dp))Row{Button(modifier=Modifier.padding(4.dp),onClick={context.startActivity(Intent(Settings.ACTION_APPLICATION_DETailS_SETTINGS).apply{data=Uri.fromParts("package",context.packageName,null)})}){Text("OpenSettings")}//Iftheydon'twanttograntpermissions,thisbuttonwillresultingoingbackButton(modifier=Modifier.padding(4.dp),onClick={onImageUri(EMPTY_IMAGE_URI)}){Text("UseCamera")}}}},){LaunchGallery()}}else{LaunchGallery()}}

之后我们修改下调用代码:

classMainActivity:ComponentActivity(){@OptIn(ExperimentalPermissionsApi::class)overridefunonCreate(savedInstanceState:Bundle?){super.onCreate(savedInstanceState)setContent{PhotoAppWithCameraX_ComposeTheme{//Asurfacecontainerusingthe'background'colorfromthethemeSurface(modifier=Modifier.fillMaxSize(),color=MaterialTheme.colors.background){varimageUribyremember{mutableStateOf(EMPTY_IMAGE_URI)}if(imageUri!=EMPTY_IMAGE_URI){Box(){Image(modifier=Modifier.fillMaxSize(),painter=rememberImagePainter(imageUri),contentDescription="Capturedimage")Button(modifier=Modifier.align(Alignment.BottomCenter),onClick={imageUri=EMPTY_IMAGE_URI}){Text("Removeimage")}}}else{varshowGallerySelectbyremember{mutableStateOf(false)}if(showGallerySelect){GallerySelect(onImageUri={uri->showGallerySelect=falseimageUri=uri})}else{Box(){CameraCapture(onImageFile={file->imageUri=file.toUri()})Button(modifier=Modifier.align(Alignment.TopCenter).padding(4.dp),onClick={showGallerySelect=true}){Text("SelectfromGallery")}}}}}}}}}valEMPTY_IMAGE_URI:Uri=Uri.parse("file://dev/null")

到这里应该可以从图库中选图片了,这样一个图片APP的一些基础功能都算演示完了,后续就是根据自己的需要进行各种扩展了,完整的代码可以访问:

您的电子邮箱地址不会被公开。必填项已用*标注

Copyright©2021-2023SEO禅专注seo优化-分享SEO技术


本文地址:https://www.badfl.com/article/f0f495d852dd183201e3.html

上一篇:Laravel8x入门开发教程Debian9服务器部署La...
下一篇:利用谷歌SearchConsole分析SEO优化方法...

发表评论

温馨提示

做上本站友情链接,在您站上点击一次,即可自动收录并自动排在本站第一位!
<a href="https://www.badfl.com/" target="_blank">自动秒收录</a>