最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android如何獲取本地文件目錄

 更新時間:2024年04月02日 11:02:42   作者:糖心荷包蛋°  
這篇文章主要介紹了Android如何獲取本地文件目錄,通過點擊按鈕,獲取本地文件目錄,可以選擇圖片,展示選取的對應(yīng)圖片和展示存儲路徑,感興趣的朋友跟隨小編一起看看吧

一、實現(xiàn)效果

一個簡單的demo。點擊按鈕,獲取本地文件目錄,可以選擇圖片,展示選取的對應(yīng)圖片和展示存儲路徑。如圖所示:

二、實現(xiàn)方式

1. 權(quán)限

AndroidManifest.xml文件里面添加權(quán)限

<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())
                // 處理返回的圖片數(shù)據(jù)
                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"
    }
}

以上就是全部內(nèi)容

主頁有更多 Android 相關(guān)文章,歡迎點贊收藏~

到此這篇關(guān)于Android如何獲取本地文件目錄的文章就介紹到這了,更多相關(guān)Android獲取本地文件目錄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

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

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

    本篇文章給大家通過原理和實例詳細(xì)講述了Android多線程中線程池ThreadPool的原理和使用,對此有興趣的朋友可以跟著參考學(xué)習(xí)下。
    2018-04-04
  • Android 編程下字庫的使用及注意事項

    Android 編程下字庫的使用及注意事項

    在安卓操作系統(tǒng)下對于 TextView 字體的支持非常有限,默認(rèn)情況下TextView的typeface屬性支持三種字體;接下來本文將會介紹Android 編程下字庫的使用及注意事項,感興趣的朋友可以了解下,希望對你有所幫助
    2013-01-01
  • Android選擇與上傳圖片之Matisse教程

    Android選擇與上傳圖片之Matisse教程

    這篇文章主要介紹了在Android中對于圖片的選擇與上傳方法,本文介紹了Matisse的相關(guān)使用教程,學(xué)習(xí)Android的同學(xué)進來看看吧
    2021-08-08
  • android學(xué)習(xí)筆記之View的滑動

    android學(xué)習(xí)筆記之View的滑動

    Android開發(fā)中我們常常需要View滑動實現(xiàn)一些絢麗的效果來優(yōu)化用戶體驗,下面這篇文章主要給大家介紹了關(guān)于android學(xué)習(xí)筆記之View滑動的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-01-01
  • Android識別NFC芯片制造商的方法

    Android識別NFC芯片制造商的方法

    這篇文章介紹了Android識別NFC芯片制造商的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-11-11
  • Android實現(xiàn)手勢滑動多點觸摸縮放平移圖片效果(二)

    Android實現(xiàn)手勢滑動多點觸摸縮放平移圖片效果(二)

    這篇文章主要介紹了Android實現(xiàn)手勢滑動多點觸摸縮放平移圖片效果,實現(xiàn)圖片支持多點觸控,自由的進行縮放、平移的注意事項,感興趣的小伙伴們可以參考一下
    2016-02-02
  • Android使用Intent獲取聯(lián)系人信息

    Android使用Intent獲取聯(lián)系人信息

    這篇文章主要為大家詳細(xì)介紹了Android Intent的使用方法,Android如何獲取聯(lián)系人信息,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • Android編程實現(xiàn)TCP客戶端的方法

    Android編程實現(xiàn)TCP客戶端的方法

    這篇文章主要介紹了Android編程實現(xiàn)TCP客戶端的方法,結(jié)合實例形式分析了Android實現(xiàn)TCP客戶端的原理及數(shù)據(jù)通信的相關(guān)技巧,需要的朋友可以參考下
    2016-04-04
  • Android性能優(yōu)化系列篇UI優(yōu)化

    Android性能優(yōu)化系列篇UI優(yōu)化

    這篇文章主要為大家介紹了Android性能優(yōu)化系列篇UI優(yōu)化示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-10-10
  • Android編程中沉浸式狀態(tài)欄的三種實現(xiàn)方式詳解

    Android編程中沉浸式狀態(tài)欄的三種實現(xiàn)方式詳解

    這篇文章主要介紹了Android編程中沉浸式狀態(tài)欄的三種實現(xiàn)方式,簡單描述了沉浸式狀態(tài)欄的概念、功能并結(jié)合實例形式詳細(xì)分析了Android實現(xiàn)沉浸式狀態(tài)欄的三種操作技巧與注意事項,需要的朋友可以參考下
    2018-02-02

最新評論

嘉义市| 毕节市| 龙州县| 三门县| 穆棱市| 临猗县| 山丹县| 鸡东县| 宜城市| 武邑县| 攀枝花市| 武夷山市| 天柱县| 武清区| 同江市| 新巴尔虎左旗| 青田县| 鹰潭市| 广昌县| 兰考县| 东乡县| 大连市| 天峻县| 阿瓦提县| 齐河县| 修武县| 九龙县| 无锡市| 正定县| 于都县| 祁连县| 进贤县| 龙州县| 搜索| 白水县| 庆阳市| 盱眙县| 柳江县| 剑阁县| 桦南县| 富阳市|