Android file names with spaces cannot be opened
•
Android
When I open a file in sdcard, my code works normally. However, if I open a file name with spaces, an error will appear (for example: path – "/ sdcard / download / Hello hi. JPG")
I tried string. Replace ("", ""); It doesn't work
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();
}
Please help
resolvent:
Try:
Uri uri = Uri.parse(paths);
File file = new File(uri.getPath());
Uri.parse fixes all spaces / backslashes / illegal characters in the path and produces a "good" URI
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码