Android如何获取本地文件目录

 更新时间:2024年04月02日 11:02:42   作者:糖心荷包蛋°  
这篇文章主要介绍了Android如何获取本地文件目录,通过点击按钮,获取本地文件目录,可以选择图片,展示选取的对应图片和展示存储路径,感兴趣的朋友跟随小编一起看看吧

一、实现效果

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

二、实现方式

1. 权限

AndroidManifest.xml文件里面添加权限

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

2. 布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <ImageView
        android:id="@+id/main_iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <Button
        android:id="@+id/main_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/file_store"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/main_iv"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/main_tx"
        tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>

3. kotlin代码

class MainActivity : AppCompatActivity() {
    private lateinit var btn2: Button
    private lateinit var ivImage: ImageView
    private lateinit var btx:TextView
    private lateinit var activityResultLauncher: ActivityResultLauncher<Intent>
    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        btn2 = findViewById(R.id.main_btn)
        ivImage = findViewById(R.id.main_iv)
        btx=findViewById(R.id.main_tx)
        activityResultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
            if (result.resultCode == RESULT_OK) {
                Log.e(this::class.java.name, "Result: " + result.data.toString())
                // 处理返回的图片数据
                val uri: Uri? = result.data?.data
                uri?.let {
                    ivImage.setImageURI(it)
                    Log.e(this::class.java.name, "Uri: $it")
                    // 获取并显示图片的路径
                    btx.text=getPathFromUri(it)
                }
            }
        }
        btn2.setOnClickListener {
            val intent = Intent(Intent.ACTION_PICK).apply {
                data = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
                type = "image/*"
            }
            activityResultLauncher.launch(intent)
        }
    }
    //返回图片的路径字符串
    private fun getPathFromUri(uri:Uri):String{
        return uri.path?:"Unknown"
    }
}

以上就是全部内容

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

到此这篇关于Android如何获取本地文件目录的文章就介绍到这了,更多相关Android获取本地文件目录内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 完全解析Android多线程中线程池ThreadPool的原理和使用

    完全解析Android多线程中线程池ThreadPool的原理和使用

    本篇文章给大家通过原理和实例详细讲述了Android多线程中线程池ThreadPool的原理和使用,对此有兴趣的朋友可以跟着参考学习下。
    2018-04-04
  • Android 编程下字库的使用及注意事项

    Android 编程下字库的使用及注意事项

    在安卓操作系统下对于 TextView 字体的支持非常有限,默认情况下TextView的typeface属性支持三种字体;接下来本文将会介绍Android 编程下字库的使用及注意事项,感兴趣的朋友可以了解下,希望对你有所帮助
    2013-01-01
  • Android选择与上传图片之Matisse教程

    Android选择与上传图片之Matisse教程

    这篇文章主要介绍了在Android中对于图片的选择与上传方法,本文介绍了Matisse的相关使用教程,学习Android的同学进来看看吧
    2021-08-08
  • android学习笔记之View的滑动

    android学习笔记之View的滑动

    Android开发中我们常常需要View滑动实现一些绚丽的效果来优化用户体验,下面这篇文章主要给大家介绍了关于android学习笔记之View滑动的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2023-01-01
  • Android识别NFC芯片制造商的方法

    Android识别NFC芯片制造商的方法

    这篇文章介绍了Android识别NFC芯片制造商的方法,文中通过示例代码介绍的非常详细。对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-11-11
  • Android实现手势滑动多点触摸缩放平移图片效果(二)

    Android实现手势滑动多点触摸缩放平移图片效果(二)

    这篇文章主要介绍了Android实现手势滑动多点触摸缩放平移图片效果,实现图片支持多点触控,自由的进行缩放、平移的注意事项,感兴趣的小伙伴们可以参考一下
    2016-02-02
  • Android使用Intent获取联系人信息

    Android使用Intent获取联系人信息

    这篇文章主要为大家详细介绍了Android Intent的使用方法,Android如何获取联系人信息,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-03-03
  • Android编程实现TCP客户端的方法

    Android编程实现TCP客户端的方法

    这篇文章主要介绍了Android编程实现TCP客户端的方法,结合实例形式分析了Android实现TCP客户端的原理及数据通信的相关技巧,需要的朋友可以参考下
    2016-04-04
  • Android性能优化系列篇UI优化

    Android性能优化系列篇UI优化

    这篇文章主要为大家介绍了Android性能优化系列篇UI优化示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-10-10
  • Android编程中沉浸式状态栏的三种实现方式详解

    Android编程中沉浸式状态栏的三种实现方式详解

    这篇文章主要介绍了Android编程中沉浸式状态栏的三种实现方式,简单描述了沉浸式状态栏的概念、功能并结合实例形式详细分析了Android实现沉浸式状态栏的三种操作技巧与注意事项,需要的朋友可以参考下
    2018-02-02

最新评论