Android kotlin語(yǔ)言實(shí)現(xiàn)刪除文件的解決方案
一、前言
在項(xiàng)目開(kāi)發(fā)過(guò)程中,尤其是需要跨平臺(tái)協(xié)作的項(xiàng)目,那么刪除用戶指定的文件的這種操作就顯得尤為重要了。但是在Android11+的操作系統(tǒng)中,權(quán)限聲明變得復(fù)雜了起來(lái),而且大多數(shù)的解決方案多為Java語(yǔ)言,kotlin語(yǔ)言的解決方案甚少,而且大多數(shù)的解決方案也沒(méi)有用。
本人也是尋求多日無(wú)果后嘗試多種解決方案拼合最終發(fā)現(xiàn)的本解決方案,在這里給廣大開(kāi)發(fā)者同志們提供一個(gè)模板,各位同志可以在本模版的基礎(chǔ)上進(jìn)行改寫,從而減少無(wú)謂查詢資料工作。
二、適用環(huán)境
語(yǔ)言:kotlin
操作系統(tǒng)版本:Android7+(本方案已經(jīng)對(duì)Android11+和Android7-10進(jìn)行了區(qū)分,可以放心使用)
三、模板內(nèi)容
1.權(quán)限申請(qǐng)
首先在AndroidManifest.xml中必須注冊(cè)這三項(xiàng)權(quán)限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" tools:ignore="ScopedStorage" />
完整的AndroidManifest.xml示例如下所示:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <application android:allowBackup="true" android:dataExtractionRules="@xml/data_extraction_rules" android:fullBackupContent="@xml/backup_rules" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.AbstractFunctionDemo" tools:targetApi="31"> <activity android:name=".MainActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" tools:ignore="ScopedStorage" /> </manifest>
2.Activity中的模板
本人在模板中按步驟寫了注釋了,廣大開(kāi)發(fā)者同志在復(fù)制走后,按照注釋的流程消化,然后進(jìn)行簡(jiǎn)單的改寫即可。
完整模板(MainActivity.kt)如下:
package com.example.abstractfunctiondemo import android.os.Bundle import androidx.activity.enableEdgeToEdge import androidx.appcompat.app.AppCompatActivity import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsCompat import android.Manifest import android.app.Activity import android.content.ContentUris import android.content.ContentValues import android.content.Context import android.content.Intent import android.content.pm.PackageManager import android.provider.Settings import android.net.Uri import android.os.Build import android.os.Environment import android.provider.DocumentsContract import android.provider.MediaStore import android.provider.OpenableColumns import android.util.Log import android.widget.Toast import androidx.activity.result.contract.ActivityResultContracts import androidx.core.app.ActivityCompat import androidx.core.content.ContextCompat import androidx.documentfile.provider.DocumentFile import com.google.android.material.button.MaterialButton import java.io.File import java.io.FileInputStream import java.io.FileOutputStream import java.io.OutputStream class MainActivity : AppCompatActivity() { /** * 1.定義選擇器的公共變量 * * 定義這個(gè)全局變量是因?yàn)楸仨氃趏nCreate的時(shí)候就注冊(cè), * 不可以現(xiàn)用現(xiàn)注冊(cè) */ private lateinit var filePickerLauncher: androidx.activity.result.ActivityResultLauncher<Intent> private var onFilePicked: ((String) -> Unit)? = null /** * 定義目標(biāo)文件路徑的公共變量 */ private var sourceFilePath: String? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() setContentView(R.layout.activity_main) ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets -> val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()) v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom) insets } /** * 5.注冊(cè)文件選擇器 */ filePickerLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> if (result.resultCode == Activity.RESULT_OK) { val uri: Uri? = result.data?.data if (uri != null) { Log.e("選取文件", "選擇的文件 URI: $uri") onFilePicked?.invoke(uri.toString()) // ? 直接返回 `Uri` deleteFileByUri(uri) // ? 直接用 `Uri` 刪除 Log.e("選取文件", "執(zhí)行結(jié)束") } else { Toast.makeText(this, "無(wú)法獲取文件", Toast.LENGTH_SHORT).show() } } else { Toast.makeText(this, "未選擇文件", Toast.LENGTH_SHORT).show() } } val testButton2: MaterialButton = findViewById(R.id.testButton2) testButton2.setOnClickListener { /** * 6.調(diào)用 */ if (checkAndRequestPermissions()) { pickFile() } } } /** * 2.定義文件選擇器激活函數(shù) */ private fun pickFile() { filePickerLauncher.launch(Intent(Intent.ACTION_OPEN_DOCUMENT).apply { addCategory(Intent.CATEGORY_OPENABLE) type = "*/*" }) } /** * 3.定義URI解析函數(shù) */ private fun getFilePathFromUri(uri: Uri): String? { val context = applicationContext var filePath: String? = null if (uri.scheme == "content") { val projection = arrayOf(MediaStore.Files.FileColumns.DATA) context.contentResolver.query(uri, projection, null, null, null)?.use { cursor -> if (cursor.moveToFirst()) { val columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATA) filePath = cursor.getString(columnIndex) } } } return filePath } /** * 4.定義文件刪除函數(shù) */ private fun deleteFileByUri(uri: Uri) { try { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { // ? Android 10+ 需要用 `DocumentsContract.deleteDocument()` val deleted = DocumentsContract.deleteDocument(contentResolver, uri) if (deleted) { Log.e("刪除文件", "文件刪除成功: $uri") Toast.makeText(this, "文件刪除成功", Toast.LENGTH_SHORT).show() } else { Log.e("刪除文件", "文件刪除失敗: $uri") Toast.makeText(this, "無(wú)法刪除文件", Toast.LENGTH_SHORT).show() } } else { // ? Android 9 及以下,嘗試轉(zhuǎn)換為文件路徑 val filePath = getFilePathFromUri(uri) if (filePath != null) { val file = File(filePath) if (file.exists() && file.delete()) { Log.e("刪除文件", "文件刪除成功: $filePath") Toast.makeText(this, "文件刪除成功", Toast.LENGTH_SHORT).show() } else { Log.e("刪除文件", "文件刪除失敗: $filePath") Toast.makeText(this, "無(wú)法刪除文件", Toast.LENGTH_SHORT).show() } } else { Toast.makeText(this, "無(wú)法獲取文件路徑,嘗試手動(dòng)刪除", Toast.LENGTH_SHORT).show() } } } catch (e: Exception) { e.printStackTrace() Toast.makeText(this, "刪除文件出錯(cuò): ${e.message}", Toast.LENGTH_SHORT).show() } } }
以上就是Android kotlin語(yǔ)言實(shí)現(xiàn)刪除文件的解決方案的詳細(xì)內(nèi)容,更多關(guān)于Android kotlin刪除文件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android Broadcast 和 BroadcastReceiver的權(quán)限限制方式
這篇文章主要介紹了Android Broadcast 和 BroadcastReceiver的權(quán)限限制方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03Android用StaticLayout實(shí)現(xiàn)文字轉(zhuǎn)化為圖片效果(類似長(zhǎng)微博發(fā)送)
這篇文章主要給大家介紹了關(guān)于Android利用StaticLayout實(shí)現(xiàn)文字轉(zhuǎn)化為圖片效果,實(shí)現(xiàn)的效果類似我們常見(jiàn)的長(zhǎng)微博效果,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),需要的朋友們下面來(lái)一起看看吧。2017-08-08Android開(kāi)發(fā)Retrofit源碼分析
這篇文章主要為大家介紹了Android開(kāi)發(fā)Retrofit源碼分析詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07Android手機(jī)聯(lián)系人帶字母索引的快速查找
這篇文章主要為大家詳細(xì)介紹了Android手機(jī)聯(lián)系人帶字母索引的快速查找實(shí)現(xiàn)方法,感興趣的小伙伴們可以參考一下2016-03-03Android 5.0 實(shí)現(xiàn)水波擴(kuò)散效果
這篇文章主要為大家詳細(xì)介紹了Android 5.0 實(shí)現(xiàn)水波擴(kuò)散效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01詳解Android 7.0 Settings 加載選項(xiàng)
本篇文章主要介紹了Android 7.0 Settings 加載選項(xiàng),Android 7.0 Settings頂部多了一個(gè)建議選項(xiàng),多了個(gè)側(cè)邊欄,操作更加便捷了,有興趣的可以了解一下。2017-02-02Android自定義ViewGroup之實(shí)現(xiàn)FlowLayout流式布局
這篇文章主要為大家詳細(xì)介紹了Android自定義ViewGroup之實(shí)現(xiàn)FlowLayout流式布局的相關(guān)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06詳解Android中PopupWindow在7.0后適配的解決
本篇文章主要介紹了詳解Android中PopupWindow在7.0后適配的解決,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05