Android Q的公共路径
创始人
2024-08-15 02:30:15
0

Android Q引入了Scoped Storage(作用域存储)的概念,对应用程序的访问权限进行了限制。在Android Q中,公共路径已经不再直接公开给应用程序使用,而是通过MediaStore API来访问。

以下是一个示例代码,展示如何使用MediaStore API来获取Android Q的公共路径:

import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.util.Log;

public class PublicPathUtils {

    private static final String TAG = "PublicPathUtils";

    public static Uri getPublicPath(Context context, String fileName) {
        Uri uri = null;
        Cursor cursor = null;
        try {
            // 查询公共图像库
            cursor = context.getContentResolver().query(
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                    new String[]{MediaStore.Images.Media._ID},
                    MediaStore.Images.Media.DISPLAY_NAME + "=?",
                    new String[]{fileName},
                    null);

            if (cursor != null && cursor.moveToFirst()) {
                long id = cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media._ID));
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    // Android Q及以上版本需要使用ContentUris工具类来获取uri
                    uri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id);
                } else {
                    // Android Q以下版本可以直接使用Uri.fromFile方法
                    uri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, String.valueOf(id));
                }
            }
        } catch (Exception e) {
            Log.e(TAG, "Failed to get public path: " + e.getMessage());
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return uri;
    }
}

上述示例代码中,我们通过查询MediaStore.Images.Media.EXTERNAL_CONTENT_URI来获取公共图像库中指定文件名的Uri。如果是Android Q及以上版本,我们使用ContentUris工具类来构建Uri,否则可以直接使用Uri.fromFile方法。

请注意,使用MediaStore API访问公共路径需要相应的权限声明,例如在AndroidManifest.xml文件中添加以下权限:


希望这个示例代码能够帮助到您!

相关内容

热门资讯

推荐十款!大菠萝手游辅助,we... 推荐十款!大菠萝手游辅助,wejoker辅助软件,总是是真的有挂(有挂头条)所有人都在同一条线上,像...
实测必看!wpk模拟器是什么,... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在这款游戏中打...
玩家亲测!wpk透视怎么安装,... 玩家亲测!wpk透视怎么安装,aapoker如何设置胜率,果然存在有挂(有挂助手)1、在插件功能辅助...
一分钟了解!约局吧怎么看有没有... 一分钟了解!约局吧怎么看有没有挂,cloudpoker怎么开挂,果然真的有挂(有挂解密)1、这是跨平...
程序员教你!wepoker手机... 程序员教你!wepoker手机版透视脚本,xpoker透视辅助,好像是有挂(今日头条)1、wepok...
一起来讨论!hhpoker有后... 一起来讨论!hhpoker有后台操作吗,wepoker私人局可以透视,竟然真的是有挂(存在有挂)是不...
热点推荐!aapoker俱乐部... 热点推荐!aapoker俱乐部靠谱吗,aapoker透视脚本,真是是真的有挂(揭秘有挂)1、破解器简...
分享实测!wepoker私人辅... 分享实测!wepoker私人辅助器,wepoker破解器有用吗,一直存在有挂(详细教程)能透视中分为...
玩家必看教程!约局吧如何查看是... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在这款游戏中打...
玩家必看科普!拱趴大菠萝万能辅... 玩家必看科普!拱趴大菠萝万能辅助器,wepoker私人局透视插件,切实是有挂(有挂方法)进入游戏-大...