我知道我问的是已经问了很多时间的问题,但我已经尝试了很多答案。
我已经开发了以下代码来获取pdf文件,我已经完美地设置了PROVIDER。
Intent intent = new Intent();
intent.setType("application/pdf");
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, PICKFILE_REQUEST_CODE);
但是,一旦我将获得有关onActivityResult
then尝试获取文件路径的数据。
所以在少数情况下,我正在获得文件路径。但在三星设备中,我将获得URI作为content://file-content/number
。因此,由于这种类型的URI,方法无法将该URI转换为文件路径。
这是我将URI转换为localPath的方法
private static String getRealPath(ContentResolver contentResolver, Uri uri, String whereClause) {
String localPath = "";
// Query the URI with the condition.
Cursor cursor = contentResolver.query(uri, null, whereClause, null, null);
if (cursor != null) {
boolean moveToFirst = cursor.moveToFirst();
if (moveToFirst) {
// Get column name by URI type.
String columnName = MediaStore.Images.Media.DATA;
if (uri == MediaStore.Images.Media.EXTERNAL_CONTENT_URI) {
columnName = MediaStore.Images.Media.DATA;
} else if (uri == MediaStore.Audio.Media.EXTERNAL_CONTENT_URI) {
columnName = MediaStore.Audio.Media.DATA;
} else if (uri == MediaStore.Video.Media.EXTERNAL_CONTENT_URI) {
columnName = MediaStore.Video.Media.DATA;
}
// Get column index.
int columnIndex = cursor.getColumnIndex(columnName);
// Get column value which is the uri related file local path.
localPath = cursor.getString(columnIndex);
}
}
return localPath;
}
所以我对这个问题有两个疑问:
content://file-content/number
uri转换为文件路径。提前致谢。
我想获取uri的列名,但是当用户从Drive的文件中选择文件时,没有获得哪种类型的URI。
使用此代码检查URI的方案:
if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
// Content scheme
} else if (uri.getScheme().equals(ContentResolver.SCHEME_FILE)){
// File scheme
} else {
// Other
}
如何将content:// file-content / number uri转换为文件路径。
创建临时文件:
@Nullable
private File createTempUploadFile(String fileName) {
File storageDir = new File(getActivity().getExternalFilesDir(null), "Upload");
if (storageDir.exists()) {
File[] files = storageDir.listFiles();
for (File file : files) {
file.delete();
}
} else {
if (!storageDir.mkdirs()) {
return null;
}
}
File file = new File(storageDir.getPath() + File.separator + fileName);
mCurrentFilePath = "file:" + file.getAbsolutePath();
return file;
}
编写一个util方法,从输入流复制到文件
public static boolean copyFileFromInputStream(InputStream inputStream, File dest) {
OutputStream outputStream = null;
try {
outputStream = new FileOutputStream(dest);
final byte[] buffer = new byte[64 * 1024]; // 64KB
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.flush();
return true;
} catch (IOException e) {
Timber.e(e);
return false;
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
} catch (IOException e) {
// Do nothing.
}
}
}
将内容从URI复制到onActivityResult
中的临时文件
try {
File dest = createTempUploadFile(fileName);
if (dest != null) {
InputStream inputStream =
getActivity().getContentResolver().openInputStream(uri);
Utils.copyFileFromInputStream(inputStream, dest);
}
} catch (Exception e) {
// TODO
}
现在从临时文件中获取文件路径。
更新:如果要从URI获取文件名,请在onActivityResult
中使用此代码。
Cursor cursor = null;
try {
cursor = getActivity().getContentResolver().query(uri,
null,
null,
null,
null);
if (cursor != null && cursor.moveToFirst()) {
int displayNameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
String fileName = cursor.getString(displayNameIndex);
}
} finally {
if (cursor != null) {
cursor.close();
}
}