Android 如何从图库或 Google 相册中获取实际文件路径
创始人
2024-08-15 14:30:15
0

在Android中,从图库或Google相册获取实际文件路径的解决方法可以通过以下步骤实现:

  1. 在AndroidManifest.xml文件中添加以下权限:

  1. 创建一个用于处理图库或Google相册返回结果的回调方法:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
        Uri uri = data.getData();
        String filePath = getRealPathFromURI(uri);
        // 使用filePath进行后续操作
    }
}
  1. 实现获取实际文件路径的方法getRealPathFromURI():
private String getRealPathFromURI(Uri uri) {
    String filePath;
    if (Build.VERSION.SDK_INT < 11)
        filePath = RealPathUtil.getRealPathFromURI_BelowAPI11(this, uri);
    else if (Build.VERSION.SDK_INT < 19)
        filePath = RealPathUtil.getRealPathFromURI_API11to18(this, uri);
    else
        filePath = RealPathUtil.getRealPathFromURI_API19(this, uri);
    return filePath;
}
  1. 创建一个RealPathUtil类来实现根据不同版本获取实际文件路径的方法:
public class RealPathUtil {

    @SuppressLint("NewApi")
    public static String getRealPathFromURI_API19(Context context, Uri uri) {
        String filePath = "";
        String wholeID = DocumentsContract.getDocumentId(uri);

        String id = wholeID.split(":")[1];

        String[] column = { MediaStore.Images.Media.DATA };
        String sel = MediaStore.Images.Media._ID + "=?";

        Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel, new String[]{ id }, null);

        int columnIndex = cursor.getColumnIndex(column[0]);

        if (cursor.moveToFirst()) {
            filePath = cursor.getString(columnIndex);
        }
        cursor.close();
        return filePath;
    }

    public static String getRealPathFromURI_API11to18(Context context, Uri contentUri) {
        String[] proj = { MediaStore.Images.Media.DATA };
        String result = null;

        CursorLoader cursorLoader = new CursorLoader(context, contentUri, proj, null, null, null);
        Cursor cursor = cursorLoader.loadInBackground();

        if (cursor != null) {
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            result = cursor.getString(column_index);
            cursor.close();
        }
        return result;
    }

    public static String getRealPathFromURI_BelowAPI11(Context context, Uri contentUri) {
        String[] proj = { MediaStore.Images.Media.DATA };
        Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        String result = cursor.getString(column_index);
        cursor.close();
        return result;
    }
}

以上代码示例中的RealPathUtil类实现了根据不同版本获取实际文件路径的方法。根据不同的API版本,分别使用了不同的方式来获取实际文件路径。在回调方法中,首先获取选择的图片的Uri,然后调用getRealPathFromURI()方法来获取实际文件路径。最后,可以使用获取到的实际文件路径进行后续的操作。

相关内容

热门资讯

分享个大家(wepoke计算)... 分享个大家(wepoke计算)外挂辅助工具(辅助挂)科技教程(2025已更新)(哔哩哔哩);科技安装...
一分钟了解!红龙扑克有挂么(辅... 自定义红龙扑克系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅助器,不管你是想分...
总算了解!轰趴大菠萝十三水辅助... 总算了解!轰趴大菠萝十三水辅助(德扑之星比赛)外挂透视辅助插件(辅助挂)细节方法(2023已更新)(...
4分钟科普!家家盘锦麻将有挂(... 4分钟科普!家家盘锦麻将有挂(辅助挂)外挂透视辅助挂(2024已更新)(哔哩哔哩)1、让任何用户在无...
如何分辨真伪(WPK计算器)软... 如何分辨真伪(WPK计算器)软件透明挂辅助脚本(透视)实用技巧(2021已更新)(哔哩哔哩)是一款可...
透视计算!wepkoe有外挂么... 透视计算!wepkoe有外挂么(辅助挂)外挂透视辅助器安装(2022已更新)(哔哩哔哩)是一款可以让...
一起来讨论!微扑克机制技巧(W... 一起来讨论!微扑克机制技巧(Wepoke稳定)外挂透视辅助机制(辅助挂)技巧教程(2021已更新)(...
8分钟普及!潮汕麻将专用神器(... 8分钟普及!潮汕麻将专用神器(透视)外挂透视辅助机制(2020已更新)(哔哩哔哩);无聊就玩这款真的...
透视存在(德扑之星解说)外挂透... 透视存在(德扑之星解说)外挂透明挂辅助APP(辅助挂)透视辅助(2023已更新)(哔哩哔哩);致您一...
一分钟了解!德州ai辅助神器怎... 一分钟了解!德州ai辅助神器怎么收费(辅助挂)其实真的有挂2022已更新)(哔哩哔哩)1、每个玩家都...