Untitled
user_3421093
plain_text
a year ago
4.1 kB
4
Indexable
package com.example.testkotlin import android.content.Intent import android.net.Uri import android.os.Bundle import android.provider.MediaStore import android.provider.Settings import android.widget.Toast import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatActivity import androidx.core.app.ActivityCompat import androidx.core.content.ContextCompat import android.content.pm.PackageManager import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.RecyclerView class MainActivity : AppCompatActivity() { private val READ_EXTERNAL_STORAGE_REQUEST_CODE = 101 private lateinit var recyclerView: RecyclerView private lateinit var videoAdapter: VideoAdapter private val videoList = mutableListOf<Uri>() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) recyclerView = findViewById(R.id.recyclerView) recyclerView.layoutManager = GridLayoutManager(this, 3) videoAdapter = VideoAdapter(videoList) recyclerView.adapter = videoAdapter checkAndRequestStoragePermission() } private fun checkAndRequestStoragePermission() { if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.READ_EXTERNAL_STORAGE), READ_EXTERNAL_STORAGE_REQUEST_CODE) } else { loadVideos() } } override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) { super.onRequestPermissionsResult(requestCode, permissions, grantResults) when (requestCode) { READ_EXTERNAL_STORAGE_REQUEST_CODE -> { if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) { loadVideos() } else { showSettingsDialog() } } } } private fun showSettingsDialog() { val builder = AlertDialog.Builder(this) builder.setTitle("Storage Permission Needed") builder.setMessage("Please allow storage access in Settings to use this feature.") builder.setPositiveButton("Settings") { dialog, which -> val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) val uri: Uri = Uri.fromParts("package", packageName, null) intent.data = uri startActivity(intent) } builder.setNegativeButton("Cancel") { dialog, which -> dialog.dismiss() closeApp() } builder.setOnDismissListener { checkAndRequestStoragePermission() } builder.create().show() } private fun closeApp() { Toast.makeText(this, "App closing due to lack of permissions", Toast.LENGTH_SHORT).show() finish() } private fun loadVideos() { val projection = arrayOf(MediaStore.Video.Media._ID, MediaStore.Video.Media.DATA) val cursor = contentResolver.query( MediaStore.Video.Media.EXTERNAL_CONTENT_URI, projection, null, null, MediaStore.Video.Media.DATE_ADDED + " DESC" ) cursor?.use { val idIndex = it.getColumnIndexOrThrow(MediaStore.Video.Media._ID) val dataIndex = it.getColumnIndexOrThrow(MediaStore.Video.Media.DATA) while (it.moveToNext()) { val id = it.getLong(idIndex) val data = it.getString(dataIndex) val contentUri = Uri.withAppendedPath(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, id.toString()) videoList.add(contentUri) } videoAdapter.notifyDataSetChanged() } } }
Editor is loading...
Leave a Comment