Android 13中的画廊访问和画廊打开问题未能正常工作(Kotlin)。

huangapple go评论82阅读模式
英文:

The problem of gallery access and gallery opening not working in Android 13 (Kotlin)

问题

The issue you're facing appears to be related to a few problems in your code. Here's a translated version of the code with some explanations:

package com.example.test

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.provider.MediaStore
import android.widget.Button
import android.widget.ImageView
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    lateinit var btn: Button
    lateinit var imageView: ImageView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        init()
    }

    fun init() {
        bindView()
        btn.setOnClickListener { checkAndOpenGallery() }
    }

    fun bindView() {
        btn = findViewById(R.id.btn)
        imageView = findViewById(R.id.imageView)
    }

    fun checkAndOpenGallery() {
        if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
            == PackageManager.PERMISSION_GRANTED
        ) {
            openGallery()
        } else {
            requestGalleryPermission.launch(Manifest.permission.READ_EXTERNAL_STORAGE)
        }
    }

    fun openGallery() {
        val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI)
        requestImageLoad.launch(intent)
    }

    private val requestGalleryPermission = registerForActivityResult(
        ActivityResultContracts.RequestPermission()
    ) { isGranted: Boolean ->
        if (isGranted) {
            openGallery()
        } else {
            Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show()
        }
    }

    private val requestImageLoad =
        registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
            if (result.resultCode == RESULT_OK && result.data != null) {
                val img = result.data?.data
                imageView.setImageURI(img)
            }
        }
}

It seems you had some HTML-encoded characters in your code (e.g., > and &). I've corrected those in the translated code.

Please ensure that you have the required permissions set in your AndroidManifest.xml file and that you have granted the necessary permissions on your device for the app to access external storage. Additionally, make sure that the image gallery is not empty on your device, as it may affect the behavior of your code when trying to pick an image from the gallery.

英文:

Why doesn't the gallery open when I click the button even when I throw a toast in the setonclicklisener code? What is the problem?
I have also placed an imageview so that when a photo is selected from the gallery, it will be placed in the imageview section, but I cannot access the gallery at all.

main_activity.kt:

package com.example.test

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.provider.MediaStore
import android.widget.Button
import android.widget.ImageView
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    lateinit var btn: Button
    lateinit var imageView:ImageView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        init()
    }
    fun init(){
        bindView()
        btn.setOnClickListener { checkAndOpenGallery() }
    }
    fun bindView(){
        btn = findViewById(R.id.btn)
        imageView = findViewById(R.id.imageView)
    }

    fun checkAndOpenGallery() {
        if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
            == PackageManager.PERMISSION_GRANTED
        ) {
            openGallery()
        } else {
            requestGalleryPermission.launch(Manifest.permission.READ_EXTERNAL_STORAGE)
        }
    }

    fun openGallery() {
        val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI)
        requestImageLoad.launch(intent)
    }


    private val requestGalleryPermission = registerForActivityResult(
        ActivityResultContracts.RequestPermission()
    ) { isGranted: Boolean ->
        if (isGranted) {
            openGallery()
        } else {
            Toast.makeText(this, "permission de deind", Toast.LENGTH_SHORT)
        }
    }

    private val requestImageLoad =
        registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
            if (result.resultCode == RESULT_OK && result.data != null) {
                val img = result.data?.data
                imageView.setImageURI(img)
            }
        }
}

activity_main.xml:

       <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="212dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="ContentDescription,ImageContrastCheck"
        tools:srcCompat="@tools:sample/avatars" />

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="196dp"
        android:text="@string/open_gallery"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

android manifest:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

I wanted the gallery to open when I click the button but nothing

答案1

得分: 1

如果您的应用面向Android 13或更高版本,并且需要访问其他应用程序创建的媒体文件,您必须请求以下一个或多个细粒度媒体权限,而不是READ_EXTERNAL_STORAGE权限:(继续)

您应该使用在Android 13 SDK(API 33)中引入的新的照片选择器。

英文:

> If your app targets Android 13 or higher and needs to access media
> files that other apps have created, you must request one or more of
> the following granular media permissions instead of the
> READ_EXTERNAL_STORAGE permission: (contd.)

(Behaviour changes: Apps targeting Android 13 or higher)

You should use the new Photo picker which was introduced with Android 13 SDK (API 33).

huangapple
  • 本文由 发表于 2023年7月10日 15:48:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76651699.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定