Q&A
前提
・機能確認用に作ったプロジェクトなので、コード量は非常に少ないです!
・アプリ立ち上げ時にボタンが一つ表示されています。
・ボタン押下することで、ストレージ内にある動画ファイル:sample.mp4を削除する程度のコードが書かれています。
・Files.exists(path)関数を使って、削除する動画ファイルが存在していることは確認済です。(なので、録画ファイルのpathは問題無いはず!)
実現したいこと
・ストレージ内にある特定の動画ファイルをボタンクリックで削除したい
発生している問題・エラーメッセージ
・Files.deleteIfExists(path)関数を使うことで録画ファイルを削除できる認識ですが、削除されていない
該当のソースコード
MainActivity.kt
Kotlin
1package com.example.delete_sample 2import androidx.appcompat.app.AppCompatActivity 3import android.os.Bundle 4import com.example.delete_sample.databinding.ActivityMainBinding 5import java.io.IOException 6import java.nio.file.Files 7import java.nio.file.Paths 8 9private lateinit var binding: ActivityMainBinding 10 11class MainActivity : AppCompatActivity() { 12 override fun onCreate(savedInstanceState: Bundle?) { 13 super.onCreate(savedInstanceState) 14 setContentView(R.layout.activity_main) 15 16 binding = ActivityMainBinding.inflate(layoutInflater) 17 val view = binding.root 18 setContentView(view) 19 20 binding.btn.setOnClickListener(){ 21 //保存されている動画ファイルのパス 22 val path = Paths.get("/storage/emulated/0/DCIM/Camera/sample.mp4") 23 //パス内に動画ファイルがあるかの確認 24 if (Files.exists(path)) { 25 //デバッグ実行でここ通っているので、パスは間違っていない? 26 println("存在します") 27 } 28 if (Files.notExists(path)) { 29 println("存在しません") 30 } 31 32 try { 33 //動画ファイルの削除 34 val result = Files.deleteIfExists(path) 35 if (result) { 36 println("削除成功") 37 }else { 38 //デバッグ実行でここ通っている。何故? 39 println("削除失敗") 40 } 41 } catch (e : IOException) { 42 e.printStackTrace() 43 } 44 } 45 } 46}
activity_main.xml
Kotlin
1<?xml version="1.0" encoding="utf-8"?> 2<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".MainActivity"> 8 9 <Button 10 android:id="@+id/btn" 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" 13 android:text="Button" 14 app:layout_constraintBottom_toBottomOf="parent" 15 app:layout_constraintEnd_toEndOf="parent" 16 app:layout_constraintStart_toStartOf="parent" 17 app:layout_constraintTop_toTopOf="parent" 18 app:layout_constraintVertical_bias="0.499" /> 19 20</androidx.constraintlayout.widget.ConstraintLayout>
試したこと
・動画ファイルのパスが間違っていないかFiles.exists(path)で垣確認
補足情報(FW/ツールのバージョンなど)
Android Studio Chipmunk | 2021.2.1 Patch 2
Build #AI-212.5712.43.2112.8815526, built on July 11, 2022
Runtime version: 11.0.12+7-b1504.28-7817840 amd64
VM: OpenJDK 64-Bit Server VM by Oracle Corporation
Windows 10 10.0
GC: G1 Young Generation, G1 Old Generation
Memory: 1280M
Cores: 8
Registry: external.system.auto.import.disabled=true
Non-Bundled Plugins: org.jetbrains.kotlin (212-1.7.10-release-333-AS5457.46)
ここにより詳細な情報を記載してください。
回答2件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
2022/09/30 22:37
2022/10/01 00:37
2022/10/01 01:57
2022/10/01 02:06
2022/10/01 04:07
2022/10/01 05:45
2022/10/01 06:36
2022/10/01 07:08
2022/10/01 07:57
2022/10/01 09:43
2022/10/01 10:29
2022/10/01 11:03
2022/10/01 12:00
2022/10/01 12:04