Android如何獲取本地文件目錄
一、實現(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)文章
解決genymotion模擬器無法聯(lián)網(wǎng)的正確方法100%成功
android 5.1版不能聯(lián)網(wǎng),三個步驟的設(shè)置就可以解決你的genymotion模擬器無法聯(lián)網(wǎng)的問題2018-03-03
Android開發(fā)筆記之:Splash的實現(xiàn)詳解
本篇文章是對Android中Splash的實現(xiàn)進行了詳細的分析介紹,需要的朋友參考下2013-05-05
Android ViewPager導(dǎo)航小圓點實現(xiàn)無限循環(huán)效果
這篇文章主要為大家詳細介紹了Android ViewPager導(dǎo)航小圓點實現(xiàn)無限循環(huán)效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08
Android實現(xiàn)從底部彈出的Dialog示例(一)
這篇文章主要介紹了Android實現(xiàn)從底部彈出的Dialog示例(一),具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-01-01
Android發(fā)布項目到j(luò)itpack的完整步驟
這篇文章主要給大家介紹了關(guān)于Android發(fā)布項目到j(luò)itpack的完整步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-01-01
Android Studio finish()方法的使用與解決app點擊“返回”(直接退出)
這篇文章主要介紹了Android Studio finish()方法的使用與解決app點擊“返回”(直接退出),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04

