Android如何獲取本地文件目錄
一、實(shí)現(xiàn)效果
一個(gè)簡(jiǎn)單的demo。點(diǎn)擊按鈕,獲取本地文件目錄,可以選擇圖片,展示選取的對(duì)應(yīng)圖片和展示存儲(chǔ)路徑。如圖所示:

二、實(shí)現(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"
}
}以上就是全部?jī)?nèi)容
主頁(yè)有更多 Android 相關(guān)文章,歡迎點(diǎn)贊收藏~
到此這篇關(guān)于Android如何獲取本地文件目錄的文章就介紹到這了,更多相關(guān)Android獲取本地文件目錄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android 指紋識(shí)別開(kāi)發(fā)實(shí)例
這篇文章主要介紹了Android6.0 指紋識(shí)別開(kāi)發(fā)實(shí)例的相關(guān)資料,需要的朋友可以參考下2016-09-09
解決genymotion模擬器無(wú)法聯(lián)網(wǎng)的正確方法100%成功
android 5.1版不能聯(lián)網(wǎng),三個(gè)步驟的設(shè)置就可以解決你的genymotion模擬器無(wú)法聯(lián)網(wǎng)的問(wèn)題2018-03-03
Android開(kāi)發(fā)筆記之:Splash的實(shí)現(xiàn)詳解
本篇文章是對(duì)Android中Splash的實(shí)現(xiàn)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
Android ViewPager導(dǎo)航小圓點(diǎn)實(shí)現(xiàn)無(wú)限循環(huán)效果
這篇文章主要為大家詳細(xì)介紹了Android ViewPager導(dǎo)航小圓點(diǎn)實(shí)現(xiàn)無(wú)限循環(huán)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Android實(shí)現(xiàn)從底部彈出的Dialog示例(一)
這篇文章主要介紹了Android實(shí)現(xiàn)從底部彈出的Dialog示例(一),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-01-01
Android發(fā)布項(xiàng)目到j(luò)itpack的完整步驟
這篇文章主要給大家介紹了關(guān)于Android發(fā)布項(xiàng)目到j(luò)itpack的完整步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01
Android Studio finish()方法的使用與解決app點(diǎn)擊“返回”(直接退出)
這篇文章主要介紹了Android Studio finish()方法的使用與解決app點(diǎn)擊“返回”(直接退出),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
Kotlin基本類(lèi)型自動(dòng)裝箱一點(diǎn)問(wèn)題剖析
這篇文章主要剖析了Kotlin基本類(lèi)型自動(dòng)裝箱的一點(diǎn)問(wèn)題,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10

