• 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 相关文章,欢迎点赞收藏~

  • 相关阅读:
    东半球最佳的身份引擎服务,诚邀探索
    Set接口和常用方法
    Java设计模式之策略模式
    基于Echarts实现可视化数据大屏物流云大数据看板页面HTML模板
    使用Python进行名片OCR(识别姓名,职务,电话,Email邮箱)
    CSS 清除浮动
    HCIP-AI语音处理理论、应用
    基于SSM的宿舍公共财产管理系统设计与实现
    秋招准备--基础知识复习--系统编程
    PHP redis list
  • 原文地址:https://blog.csdn.net/Waterme10n/article/details/137240886