You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android项目中Picasso 2.5.2无法加载外部存储图片求助

解决Picasso 2.5.2无法加载外置SD卡图片的问题

看起来你踩了Picasso老版本的存储适配坑——2.5.2是比较早期的版本,对外置SD卡的二级存储路径(比如你提到的/storage/0403-0201/...)支持有限,而Glide的文件加载逻辑更适配新的存储场景,所以能正常加载。咱们一步步来解决这个问题:

先排查隐藏的加载异常

首先给Picasso加上错误监听,很多时候日志没报错是因为异常被吞掉了,这能帮你精准定位问题:

Picasso.with(context)
        .load(new File("/storage/0403-0201/DCIM/Camera/IMG_20180309_120329.jpg"))
        .listener(new Picasso.Listener() {
            @Override
            public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) {
                // 打印异常信息,看具体是权限还是路径适配问题
                exception.printStackTrace();
            }
        })
        .into(yourGridViewImageView);

核心解决方案:改用Content Uri加载(推荐)

外置SD卡的文件直接用绝对路径会被Picasso的默认加载逻辑拦截,换成MediaStore提供的Content Uri就能绕过这个限制。写个工具方法根据文件路径生成对应的Content Uri:

public static Uri getImageContentUri(Context context, String imagePath) {
    Cursor cursor = context.getContentResolver().query(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            new String[]{MediaStore.Images.Media._ID},
            MediaStore.Images.Media.DATA + " = ?",
            new String[]{imagePath},
            null);

    if (cursor != null && cursor.moveToFirst()) {
        int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
        cursor.close();
        return Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, String.valueOf(id));
    } else {
        // 如果文件没在MediaStore索引中,手动插入一条记录
        if (new File(imagePath).exists()) {
            ContentValues values = new ContentValues();
            values.put(MediaStore.Images.Media.DATA, imagePath);
            return context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        } else {
            return null;
        }
    }
}

然后用这个Uri加载图片:

Uri contentUri = getImageContentUri(context, "/storage/0403-0201/DCIM/Camera/IMG_20180309_120329.jpg");
if (contentUri != null) {
    Picasso.with(context).load(contentUri).into(yourGridViewImageView);
}

适配Android 10+的分区存储

如果你的应用运行在Android 10及以上系统,需要在AndroidManifest.xml的<application>标签里添加兼容标记,让应用可以继续使用旧的文件访问逻辑:

<application
    ...
    android:requestLegacyExternalStorage="true">
    ...
</application>

这个标记会暂时绕过分区存储的限制,完美适配依赖绝对路径的老版本Picasso。

最后再确认权限配置

虽然Glide能加载,但还是检查下权限是否到位:

  • 在AndroidManifest.xml中添加读权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  • 如果是Android 6.0及以上设备,记得动态申请READ_EXTERNAL_STORAGE权限。

总结

最稳妥的方案是改用Content Uri加载,加上分区存储兼容标记,既能解决Picasso的加载问题,也能兼容新系统的存储规则。如果还是有问题,可以考虑把Picasso升级到较新的版本(比如2.71828),新版本对存储路径的支持会更完善。

内容的提问来源于stack exchange,提问作者Sumit Saurabh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 09:00:36