Android的文件名以空格无法打开 [英] android filename with white space cannot be opened

查看:364
本文介绍了Android的文件名以空格无法打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我code正常工作,而在SD卡打开文件。但是,如果我打开带有空格的文件名,然后出现错误(例如路径 - / SD卡/下载/你好hi.jpg)。

Hi My code works correctly while opening a file in sdcard. but if i open a filename with white space then error occurs (example Path - "/sdcard/download/hello hi.jpg").

我试图与string.replace(,%20);这是行不通的。

I tried string.replace(" ","%20"); it does n'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();
            }

请帮忙

推荐答案

您需要逃避的空间。尝试更换,用\

You need to escape the spaces. Try replacing " " with "\ "

这篇关于Android的文件名以空格无法打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆