• Android获取本地文件目录


    一、实现效果

    一个简单的demo。点击按钮,获取本地文件目录,可以选择图片,展示选取的对应图片和展示存储路径。如图所示:

    二、实现方式

    1. 权限

    AndroidManifest.xml文件里面添加权限

    1. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    2. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    2. 布局

    1. "1.0" encoding="utf-8"?>
    2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:app="http://schemas.android.com/apk/res-auto"
    4. xmlns:tools="http://schemas.android.com/tools"
    5. android:layout_width="match_parent"
    6. android:layout_height="match_parent"
    7. tools:context=".MainActivity">
    8. <ImageView
    9. android:id="@+id/main_iv"
    10. android:layout_width="wrap_content"
    11. android:layout_height="wrap_content"
    12. app:layout_constraintBottom_toBottomOf="parent"
    13. app:layout_constraintEnd_toEndOf="parent"
    14. app:layout_constraintStart_toStartOf="parent"
    15. app:layout_constraintTop_toTopOf="parent" />
    16. <Button
    17. android:id="@+id/main_btn"
    18. android:layout_width="wrap_content"
    19. android:layout_height="wrap_content"
    20. android:text="@string/file_store"
    21. android:textSize="20sp"
    22. app:layout_constraintBottom_toBottomOf="parent"
    23. app:layout_constraintEnd_toEndOf="parent"
    24. app:layout_constraintStart_toStartOf="parent"
    25. app:layout_constraintTop_toTopOf="@+id/main_iv"/>
    26. <TextView
    27. android:layout_width="wrap_content"
    28. android:layout_height="wrap_content"
    29. android:id="@+id/main_tx"
    30. tools:ignore="MissingConstraints" />
    31. androidx.constraintlayout.widget.ConstraintLayout>

    3. kotlin代码

    1. class MainActivity : AppCompatActivity() {
    2. private lateinit var btn2: Button
    3. private lateinit var ivImage: ImageView
    4. private lateinit var btx:TextView
    5. private lateinit var activityResultLauncher: ActivityResultLauncher
    6. @SuppressLint("MissingInflatedId")
    7. override fun onCreate(savedInstanceState: Bundle?) {
    8. super.onCreate(savedInstanceState)
    9. setContentView(R.layout.activity_main)
    10. btn2 = findViewById(R.id.main_btn)
    11. ivImage = findViewById(R.id.main_iv)
    12. btx=findViewById(R.id.main_tx)
    13. activityResultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
    14. if (result.resultCode == RESULT_OK) {
    15. Log.e(this::class.java.name, "Result: " + result.data.toString())
    16. // 处理返回的图片数据
    17. val uri: Uri? = result.data?.data
    18. uri?.let {
    19. ivImage.setImageURI(it)
    20. Log.e(this::class.java.name, "Uri: $it")
    21. // 获取并显示图片的路径
    22. btx.text=getPathFromUri(it)
    23. }
    24. }
    25. }
    26. btn2.setOnClickListener {
    27. val intent = Intent(Intent.ACTION_PICK).apply {
    28. data = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
    29. type = "image/*"
    30. }
    31. activityResultLauncher.launch(intent)
    32. }
    33. }
    34. //返回图片的路径字符串
    35. private fun getPathFromUri(uri:Uri):String{
    36. return uri.path?:"Unknown"
    37. }
    38. }

    以上就是全部内容

    主页有更多 Android 相关文章,欢迎点赞收藏~

  • 相关阅读:
    clipStudioPaint插件开发之介绍
    【异常错误】WSL2设置为全核cpu和全部内存
    java面试题 --- Redis②
    在字节做测试5年,7月无情被辞,想给划水的兄弟提个醒
    FPGA之旅设计第五例-----IIC通信
    Springboot足球运动员训练计划管理系统的设计与实现 毕业设计-附源码281444
    Tomcat 的连接器是如何设计的(上)
    AMS:ActivityRecord 理解
    SpringBoot-属性绑定和bean属性校验
    猿创征文 【SpringBoot】SSM“加速器”SpringBoot初体验
  • 原文地址:https://blog.csdn.net/Waterme10n/article/details/137240886