问题:
最近在开发文件查看器中出现一个问题: 接收到一个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.app.common.utils; import android.content.Context; import android.database.Cursor; import android.net.Uri; import android.provider.MediaStore; import android.util.Log; import java.io.File; /** * 文件阅读器工具类 * @author zhanghao */ public class ReaderUtils { public static final String TAG = "ReaderUtils"; /** * 查询内容解析器,找到文件存储地址 *ef: android中转换content://media/external/images/media/539163为/storage/emulated/0/DCIM/Camera/IMG_20160807_123123.jpg *
把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); }
有疑问可以留言交流哦!