一次性带你解决android 7.0之前所有相册选择图片问题

首先Manifest如下

<provider
    android:name=".provider.FileProviderAndroidN"
    android:authorities="com.haha.hehe"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_path"/>
</provider>
provider name就是你的package路径+FileName,你可以抽到你的拓展包里

authorites,保证不重复就好了,一般都是直接你的包名.xxx


res下建一个xml foler然后

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path
        name="files_root"
        path="Android/data/com.haha.hehe/"/>
    <files-path
        name="a"
        path="files"/>
    <cache-path
        name="b"
        path="caches"/>
    <external-cache-path
        name="c"
        path="excaches"/>
    <external-files-path
        name="d"
        path="exfile"/>
</paths>
顾名思义,context都有相应方法 path为该目录下相对位置 name随便定

具体参考:https://developer.android.com/reference/android/support/v4/content/FileProvider.html


拍照:

val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
intent.putExtra(MediaStore.EXTRA_OUTPUT , getUri(file))
startActivityForResult(intent , CAPTURE_IMG)

fun getUri(file : File) : Uri? {
    try {
        return if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
            FileProvider.getUriForFile(this , packageName , file)
        } else {
            Uri.fromFile(file)
        }
    } catch (e : Exception) {
        e.printStackTrace()
    }
    return null
}

选图片:

val p = Intent(Intent.ACTION_PICK ,
               android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
startActivityForResult(p , CHOOSE_IMG_REQUEST_CODE)


图片处理onActivityResult:

CHOOSE_IMG_REQUEST_CODE -> {
    if (null == data) return
    val uri = data.data
    val path = getAlbumPhotoPath(uri)
    if (TextUtils.isEmpty(path)) {
        return
    }
    selectedImagePath = path
}
CAPTURE_IMG -> {
    selectedImagePath = file.absolutePath
}


fun getAlbumPhotoPath(uri : Uri) : String {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && DocumentsContract.isDocumentUri(this , uri)) {
        if (isExternalStorageDocument(uri)) {
            val docId = DocumentsContract.getDocumentId(uri)
            val split = docId.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
            val type = split[0]
            if ("primary".equals(type , ignoreCase = true)) {
                return Environment.getExternalStorageDirectory().toString() + "/" + split[1]
            }
        } else if (isDownloadsDocument(uri)) {
            val id = DocumentsContract.getDocumentId(uri)
            val contentUri = ContentUris.withAppendedId(
                    Uri.parse("content://downloads/public_downloads") , java.lang.Long.valueOf(id)!!)
            return getDataColumn(this , contentUri , null , null)
        } else if (isMediaDocument(uri)) {
            val docId = DocumentsContract.getDocumentId(uri)
            val split = docId.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
            val type = split[0]
            var contentUri : Uri? = null
            if ("image" == type) {
                contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
            } else if ("video" == type) {
                contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI
            } else if ("audio" == type) {
                contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
            }
            val selection = "_id=?"
            val selectionArgs = arrayOf(split[1])
            return getDataColumn(this , contentUri , selection , selectionArgs)
        }
    } else if ("content".equals(uri.scheme , ignoreCase = true)) {
        return if (isGooglePhotosUri(uri)) uri.lastPathSegment else getDataColumn(this , uri , null , null)
    } else if ("file".equals(uri.scheme , ignoreCase = true)) {
        return uri.path
    }
    return ""
}

ok,大功告成,7.0以下通用,之所以7.0以下是因为不确定之后会不会再次有改动,反正8.0没改,就先用着吧



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
状态栏的问题是什么?为什么在7.0以上的手机上运行时感觉没有沉浸式状态栏了?如何解决这个问题? 回答: 在Android 7.0以上的手机上,出现了一个问题,即无论怎么修改状态栏背景色都没有效果,看起来不再是沉浸式的状态栏。这个问题的原因是在Android 7.0之前,状态栏的实现是通过DecorView这个类来完成的,而在7.0以上,DecorView变成了一个独立的类,并且增加了一些新的属性和方法。其中一个新的属性是mSemiTransparentStatusBarColor,这个属性可能与我们要解决问题有关。\[1\]\[2\]\[3\] 要解决这个问题,我们可以尝试修改mSemiTransparentStatusBarColor属性的值,以达到我们想要的状态栏效果。具体的实现方法可能需要根据具体的应用程序和代码来进行调整。可以通过查看相关的文档和源代码来了解如何正确地修改这个属性。 #### 引用[.reference_title] - *1* *2* [android状态栏半透明灰色,快速解决Android7.0下沉浸式状态栏变灰的问题](https://blog.csdn.net/weixin_35792040/article/details/117493339)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [android状态栏半透明灰色,Android7.0沉浸式状态栏蒙灰问题完美解决](https://blog.csdn.net/weixin_34493827/article/details/117493345)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值