在 SD 卡中打开文件时,我的代码可以正常工作。但是,如果我打开带有空格的文件名,则会发生错误(例如:路径 - “/sdcard/download/hello hi.jpg”)。
我尝试过 string.replace(" ","%20");它不起作用
try {
File file = new File(paths);
if (file.exists()) {
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(paths));
if (!mimeType.equals("")) {
intent.setDataAndType(path, mimeType);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
} else {
Toast.makeText(this, "Unsupported Format to Open", Toast.LENGTH_SHORT).show();
}
}
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "No Application Available to View this File", Toast.LENGTH_SHORT).show();
} catch(Exception e) {
Toast.makeText(this, "Error Occurred", Toast.LENGTH_SHORT).show();
}
请帮忙
Try:
Uri uri = Uri.parse(paths);
File file = new File(uri.getPath());
Uri.parse
修复路径中的所有空格/反斜杠/非法字符问题并生成“良好”的 uri。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)