API 29弃用了ALBUM_ART列,依此类推,如何获取路径? [英] ALBUM_ART column is deprecated from API 29 and so on, how to obtain path?

查看:158
本文介绍了API 29弃用了ALBUM_ART列,依此类推,如何获取路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们当前正在使用以下代码获取专辑封面的路径: MediaStore.Audio.AlbumColumns.ALBUM_ART ,并且已成功获取该路径,但像素3a(Android 10)除外.经过一番研究后,ALBUM_ART已弃用API 29及以上版本,如下所示:在这里

We are currently obtaining the path of album art using: MediaStore.Audio.AlbumColumns.ALBUM_ART, and is successfully obtaining the path, except on pixel 3a (Android 10). After some research, the ALBUM_ART became deprecated API 29 and over as shown: Here

在此链接中说:应用程序可能没有文件系统权限来直接访问此路径.应用程序应尝试使用ContentResolver#loadThumbnail获得访问权限,而不是尝试直接打开此路径."

In this link it says: "Apps may not have file system permissions to directly access this path. Instead of trying to open this path directly, apps should use ContentResolver#loadThumbnail to gain access."

我的问题是:
1)我已经在应用程序清单上声明了对外部存储访问的权限(READ_EXTERNAL_STORAGE),并且在应用程序内导航时正在请求权限.我必须提供哪些权限才能访问专辑封面以获得路径?

My questions are:
1) I'm already stating on the application manifest the permissions for external storage access (READ_EXTERNAL_STORAGE) and is requesting permission while navigating in-app. Which permissions do i have to provide to allow access to album art in order to obtain the path?

2)我似乎无法在线找到loadThumbnail上的任何内容(甚至在我使用目标和编译SDK 29时也无法通过代码在ContentResolver类上找到内容),如果1)无法完成,那怎么办我使用loadThumbnail,为什么它没有显示在代码上?

2) I can't seem to find any content on loadThumbnail online (and not even on ContentResolver class through code, while i am using target and compile SDK 29), if 1) can't be done, then how do i use loadThumbnail and why it's not showing on code?

提前谢谢.

推荐答案

为了使用ContentResolver的方法,请确保已安装最新的SDK和相关工具,并在代码中首先实例化ContentResolver对象,然后使用相应地:

In order to use the method of ContentResolver, make sure you have the latest SDK and relevant tools installed, and in your code first instantiate a ContentResolver object and then use it accordingly:

public class MainActivity extends AppCompatActivity {
    public ContentResolver resolver;
    Bitmap albumArt;
    Size size;
    Uri uriOfItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        resolver = new ContentResolver(this) {
            @NonNull
            @Override
            public Bitmap loadThumbnail(@NonNull Uri uri, @NonNull Size size, @Nullable CancellationSignal signal) throws IOException {
                return super.loadThumbnail(uri, size, signal);
            }
        };
        //uriOfItem = uri of your file
        size = new Size(100, 100);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            try {
                albumArt = resolver.loadThumbnail(uriOfItem, size, null);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

关于您的第一个问题,@ Rj_Innocent_Coder是否不介意我在此处包括他的评论:

when it comes to your first question if @Rj_Innocent_Coder doesn't mind me including his comment here:

作为Android Q范围存储功能的一部分,Google宣布SAF(存储访问框架)将取代常规存储权限.这意味着,即使您尝试使用存储权限,也只会授予访问特定类型文件的权限,以使用要使用的文件和文件路径

As part of the scoped-storage feature on Android Q, Google announced that SAF (storage access framework) will replace the normal storage permissions. This means that even if you will try to use storage permissions, it will only grant to access to specific types of files for File and file-path to be used

@ hetoan2发表评论后,我再次检查了文档,发现ContentResolver是抽象的,因此无法将 ContentResolver.loadThumbnail()用作方法调用.这意味着在一个活动中,您也可以简单地使用以下内容:

EDIT 2: after @hetoan2 's comment I check the documentation again and I noticed that ContentResolver is abstract hence not being able to use ContentResolver.loadThumbnail() as a method call. That means that within an activity you could simply use the following as well:

Bitmap albumArt = getContentResolver().loadThumbnail(uriOfFile, sizeOfAreaThatDisplaysThumbnail, cancellationSignalOrNull);

这篇关于API 29弃用了ALBUM_ART列,依此类推,如何获取路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆