Android Uri转File path路径,Kotlin

Android Uri转File path路径,Kotlin

    /**
     * URI转化为file path路径
     */
    private fun getFilePathFromURI(context: Context, contentURI: Uri): String? {
        val result: String?
        var cursor: Cursor? = null

        try {
            cursor = context.contentResolver.query(contentURI, null, null, null, null)
        } catch (e: Exception) {
            e.printStackTrace()
        }

        if (cursor == null) {
            result = contentURI.path
        } else {
            cursor.moveToFirst()
            val idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA)
            result = cursor.getString(idx)
            cursor.close()
        }

        return result
    }

Android 13手机图片存储File路径转Uri,Java_android file 转uri-CSDN博客文章浏览阅读682次。android根据图片资源的drawable id转化为Uri,java_zhangphil的博客-CSDN博客。android根据图片资源的drawable id转化为Uri的工具方法。Android 13手机图片存储File路径转Uri,Java。_android file 转urihttps://blog.csdn.net/zhangphil/article/details/129640812android根据图片资源的drawable id转化为Uri,java_android drawable 转uri-CSDN博客文章浏览阅读649次。android根据图片资源的drawable id转化为Uri的工具方法。_android drawable 转urihttps://blog.csdn.net/zhangphil/article/details/129431755

Android4.4以上版本Uri content转换成File文件路径_android content 转 file-CSDN博客文章浏览阅读4.4k次,点赞2次,收藏8次。public static String getFilePathByUri(Context context, Uri uri) { String path = null; // 以 file:// 开头的 if (ContentResolver.SCHEME_FILE.equals(uri.getScheme())) { ..._android content 转 filehttps://blog.csdn.net/zhangphil/article/details/87859320

以下是一个使用Kotlin编写的示例代码,用于允许用户更改其头像。它将打开一个对话框,让用户选择从相机拍照或从相册中选择。选择后,它将更新用户的头像。 ``` private val REQUEST_IMAGE_CAPTURE = 1 private val REQUEST_IMAGE_SELECT = 2 private var mCurrentPhotoPath: String = "" private fun changeAvatar() { val pictureDialog = AlertDialog.Builder(this) pictureDialog.setTitle("选择头像") val pictureDialogItems = arrayOf("从相册选择", "拍照") pictureDialog.setItems(pictureDialogItems ) { _, which -> when (which) { 0 -> choosePhotoFromGallery() 1 -> takePhoto() } } pictureDialog.show() } private fun choosePhotoFromGallery() { val galleryIntent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI) startActivityForResult(galleryIntent, REQUEST_IMAGE_SELECT) } private fun takePhoto() { val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE) if (takePictureIntent.resolveActivity(packageManager) != null) { val photoFile: File? = try { createImageFile() } catch (ex: IOException) { null } photoFile?.also { val photoURI: Uri = FileProvider.getUriForFile( this, "com.example.android.fileprovider", it ) takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI) startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE) } } } @Throws(IOException::class) private fun createImageFile(): File { // Create an image file name val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date()) val storageDir: File = getExternalFilesDir(Environment.DIRECTORY_PICTURES)!! return File.createTempFile( "JPEG_${timeStamp}_", /* prefix */ ".jpg", /* suffix */ storageDir /* directory */ ).apply { // Save a file: path for use with ACTION_VIEW intents mCurrentPhotoPath = absolutePath } } override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) if (resultCode == Activity.RESULT_OK) { when (requestCode) { REQUEST_IMAGE_CAPTURE -> { val file = File(mCurrentPhotoPath) val uri = Uri.fromFile(file) updateAvatar(uri) } REQUEST_IMAGE_SELECT -> { val uri = data?.data updateAvatar(uri) } } } } private fun updateAvatar(uri: Uri?) { // Do something with the selected image } ``` 请注意,在使用相机拍照时,我们需要在`AndroidManifest.xml`文件中添加以下内容,以确保我们的应用程序具有访问相机的权限: ``` <uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" /> ``` 同时,我们还需要添加以下内容,以确保我们的应用程序具有访问存储的权限: ``` <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhangphil

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值