Android开发中content://与file://真实文件路径的转换

编程知识 更新时间:2023-04-05 05:33:45

问题:

最近在开发文件查看器中出现一个问题: 接收到一个uri: content://media/external/images/media/2283  获取到的getPath: /external/images/media/2283, 打开文件是出现异常:

java.io.FileNotFoundException: open failed: ENOENT (No such file or directory)
at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:144)
at android.content.ContentProviderProxy.openTypedAssetFile(ContentProviderNative.java:698)
at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1416)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1253)
at android.content.ContentResolver.openInputStream(ContentResolver.java:973)
 

解决方案:

设法把content uri转化为真是文件路径,即可使用FIleInputStream获取输入流打开文件,代码如下:

ReaderUtils.java工具类:

package com.hulk.appmon.utils;

import android.content.Context;
import android.database.Cursor;
import android.Uri;
import android.provider.MediaStore;
import android.util.Log;

import java.io.File;

/**
 * 文件阅读器工具类
 * @author zhanghao
 */
public class ReaderUtils {

    public static final String TAG = "ReaderUtils";

    /**
     * 查询内容解析器,找到文件存储地址
     * <p>ef: android中转换content://media/external/images/media/539163为/storage/emulated/0/DCIM/Camera/IMG_20160807_123123.jpg
     * <p>把content://media/external/images/media/X转换为file:///storage/sdcard0/Pictures/X.jpg
     * @param context
     * @param contentUri
     * @return
     */
    public static String getRealPathFromUri(Context context, Uri contentUri) {
        Cursor cursor = null;
        try {
            Log.i(TAG, "getRealPathFromUri: " + contentUri);
            String[] proj = { MediaStore.Images.Media.DATA };
            cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
            if (cursor != null && cursor.getColumnCount() > 0) {
                cursor.moveToFirst();
                int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                String path = cursor.getString(column_index);
                Log.i(TAG, "getRealPathFromUri: column_index=" + column_index + ", path=" + path);
                return path;
            } else {
                Log.w(TAG, "getRealPathFromUri: invalid cursor=" + cursor + ", contentUri=" + contentUri);
            }
        } catch (Exception e) {
            Log.e(TAG, "getRealPathFromUri failed: " + e  + ", contentUri=" + contentUri, e);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return "";
    }

    /**
     * 获取完整文件名(包含扩展名)
     * @param filePath
     * @return
     */
    public static String getFilenameWithExtension(String filePath) {
        if (filePath == null || filePath.length() == 0) {
            return "";
        }
        int lastIndex = filePath.lastIndexOf(File.separator);
        String filename = filePath.substring(lastIndex + 1);
        return filename;
    }

    /**
     * 判断文件路径的文件名是否存在文件扩展名 eg: /external/images/media/2283
     * @param filePath
     * @return
     */
    public static boolean isFilePathWithExtension(String filePath) {
        String filename = getFilenameWithExtension(filePath);
        return filename.contains(".");
    }
}

具体用法举例:

documentType = DocumentType.Type.Text;
        try{
            mFilePath = uri.getPath();
            //问题: 当uri为 content://media/external/images/media/2283 时, path为 /external/images/media/2283
            // 此时getOfficeTypeByFileName函数无法扥根据后缀名获取文件类型,后面的加载文件直接报错误
            //解决方案,根据url的path判断文件是否存在后缀名,如果不存在后缀名需要从系统数据库中获取真正的文件路径
            isFilePathWithExtension = ReaderUtils.isFilePathWithExtension(mFilePath);
            if (!isFilePathWithExtension) {
                String path = ReaderUtils.getRealPathFromUri(getApplicationContext(), uri);
                Log.w(TAG, "onCreate: Got real file path " + path + " from uri " + uri);
                if (!TextUtils.isEmpty(path)) {
                    mFilePath = path;
                }
            }
            documentType = DocumentUtils.getOfficeTypeByFileName(mFilePath);
            fileName = getFileName(mFilePath);
        }catch (Exception e){
            Log.e(TAG, "Get file info error:" + e + ",  uri: " + uri + ", url path: " + mFilePath, e);
        }

此处省略无数行......................

try {
    Log.i(TAG, "Start loadDocument uri:" + uri + ", documentType:" + documentType);
    IDocumentController mDocumentsContract = mDocumentView.getDocumentController(this);
    //mDocumentsContract.loadDocument(getContentResolver().openInputStream(uri), documentType, ENABLE_COPY);
    if (!isFilePathWithExtension && !TextUtils.isEmpty(mFilePath)) {
        //java.io.FileNotFoundException: open failed: ENOENT (No such file or directory),
        // uri:content://media/external/images/media/2283, documentType:0
        //此处优先使用文件输入流加载,避免出现上述异常
        Log.w(TAG, "onCreate: Load by FileInputStream from file path: " + mFilePath);
        FileInputStream in = new FileInputStream(mFilePath);
        if (in != null) {
            Log.w(TAG, "onCreate: loadDocument InputStream available: " + in.available());
            mDocumentsContract.loadDocument(in, documentType, ENABLE_COPY);
        } else {
            mDocumentsContract.loadDocument(getContentResolver().openInputStream(uri), documentType, ENABLE_COPY);
        }
    } else {
        mDocumentsContract.loadDocument(getContentResolver().openInputStream(uri), documentType, ENABLE_COPY);
    }
} catch (Exception e) {
    Log.e(TAG, "loadDocument error:" + e + ",  uri:" + uri + ", documentType:" + documentType, e);
}

有疑问可以留言交流哦!

更多推荐

Android开发中content://与file://真实文件路径的转换

本文发布于:2023-04-05 05:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/f45bef7ec21b13ea5d66266aa32f5fb7.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路径   真实   文件   Android   content

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!

  • 45035文章数
  • 14阅读数
  • 0评论数