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文件中添加以下权限:


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

相关内容

热门资讯

透视推荐!pokemmo脚本手... 透视推荐!pokemmo脚本手机版(透视)wepoker有用吗,教程烘培(今日头条)-哔哩哔哩1、起...
pokermaster破解版!... pokermaster破解版!wepoker提高好牌率(透视)app-确实揭幕是真的挂1、下载好we...
透视开挂!菠萝辅助器免费版的特... 透视开挂!菠萝辅助器免费版的特点(透视)智星德州辅助译码插件靠谱吗,教程模块(有挂技术)-哔哩哔哩1...
wpk软件是真的吗!红龙pok... wpk软件是真的吗!红龙poker辅助器免费观看(透视)教程-果然详情是真的挂1、红龙poker辅助...
hhpoker软件靠谱吗!hh... hhpoker软件靠谱吗!hhpoker辅助靠谱吗(透视)工具-原来开挂存在有挂1、首先打开hhpo...
透视详细!wepoker代打辅... 您好,wpk透视怎么安装这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多...
wepoker辅助器如何使用!... wepoker辅助器如何使用!德州hhpoker是真的吗(透视)软件-切实分享是真的挂小薇(辅助器软...
透视曝光!wepoker透视挂... 透视曝光!wepoker透视挂底牌(透视)fishpoker透视,教程绝活(有挂技术)-哔哩哔哩1、...
wepoker公共底牌!wep... wepoker公共底牌!wepoker怎么看底牌(透视)挂-都是详情真的是有挂小薇(辅助器软件下载)...
wepoker开辅助能查到吗!... wepoker开辅助能查到吗!pokemmo辅助器(透视)器-其实解密真的有挂1)wepoker开辅...